python - 将 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)
解决方案
为了给它一些时间来做事情,我有几种方法可以解决这个问题。
一种是设置时间加载页面。
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]"))
希望这会有所帮助!祝你好运
推荐阅读
- c# - 操作无法完成,因为 DbContext 已被释放。System.InvalidOperationException
- php - 以编程方式创建 WooCommerce 订单时设置外部订单 ID
- spring-boot - Spring Data JPA 自定义计数查询未显示所有记录
- kml - 使 KML LineString 跟随道路
- python - Pandas Dataframe pd.to_datetime 的日期格式完全错误
- sql - 根据条件批量插入表格
- ios - 快速如何开发执行实际蓝牙对的应用程序
- powershell - 使用 Powershell 来自非常大的文本文件的子字符串
- intellij-idea - IntelliJ 编辑器选项卡:最近一次使用
- java - 如何从 Firestore 获取作为对象的字段?