首页 > 解决方案 > 将 html 文档注入 selenium webdriver

问题描述

我正在测试一个网站,但每次都需要我登录,所以我将要测试的网页保存到了 html 文档中。

我试图通过 driver.get('file:///pp.html') 打开它,但到目前为止它只是打开文件然后关闭,以下代码均无效:

rows = driver.find_elements_by_css_selector("table.aui tr") for row in rows: projectNames = row.find_elements_by_xpath(".//td[1]") for projectName in projectNames: print (projectName.text)

标签: pythonselenium-webdriver

解决方案


为了给它一些时间来做事情,我有几种方法可以解决这个问题。

一种是设置时间加载页面。

driver = set_page_load_timeout(10) 

也许我也会使用 time 模块中的 time.sleep 命令

rows = driver.find_elements_by_css_selector("table.aui tr") 
for row in rows:
    time.sleep(2) 
    projectNames = row.find_elements_by_xpath(".//td[1]")
        for projectName in projectNames: 
        time.sleep(1)
        print (projectName.text)
        time.sleep(1)

最后,如果您的驱动程序即将关闭,您应该进入 WebDriverWait() 命令。也许是这样的

from selenium.webdriver.support import expected_conditions as EC
row = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.xpath(".//td[1]"))

希望这会有所帮助!祝你好运


推荐阅读