html_doc = """ <html><head><title>The Dormouse's story</title></head> <body> <p class="title"><b>The Dormouse's story</b></p> <p class="story">Once upon a time there were three little sisters; and their names were <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>, <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>; and they lived at the bottom of a well.</p> <p class="story">...</p> </body> </html> """
text = driver.find_element_by_tag_name('h1').text print(text)
3. 常用功能
3.1 等待页面加载
有时页面元素需要时间加载,Selenium 提供了两种等待机制:
隐式等待: 在设置的时间内等待元素加载完成。
1
driver.implicitly_wait(10) # 等待最多10秒
显式等待: 明确等待某个条件满足。
1 2 3 4 5 6 7
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, 'myElement')) )
from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "element-id")) )
intmain(){ // 创建一个名为 "file_logger" 的日志记录器 auto logger = spdlog::basic_logger_mt("file_logger", "logs.txt");
// 使用记录器进行日志记录 logger->info("This is an info message");
// 在其他地方获取并使用相同的记录器 auto same_logger = spdlog::get("file_logger"); if (same_logger) { same_logger->warn("This is a warning message from the same logger"); } else { spdlog::error("Logger not found"); }
console: Where to launch the debug target. Defaults to ‘internalConsole’ if not defined.
console: 需要调试的程序运行的地方。如果没有定义,默认为 internalConsole
console的值有:
externalTerminal:
Console applications will be launched in an external terminal window. The window will be reused in relaunch scenarios and will not automatically disappear when the application exits.
Output to the VS Code Debug Console. This doesn’t support reading console input (ex:’std::cin’ or ‘scanf’).
输出到VSCode调试窗口。这个不支持从窗口读取数据,例如 std::cin或 scanf
newExternalWindow
Console applications will be launched in their own external console window which will end when the application stops. Non-console applications will run without a terminal, and stdout/stderr will be ignored.