将html文档注入selenium webdri

2024-10-02 10:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试一个网站,但它要求我每次登录,所以我保存了网页,我想测试到一个html文档。你知道吗

我试着打开它驱动程序。获取('文件:///网页.html)但是现在它只是打开文件然后关闭,以下代码都不起作用:

行=driver.find_elements_by_css_selector("表.auitr“) 对于行中的行: 项目名称=行。按xpath查找元素(“//td[1]”) 对于projectNames中的projectName: 打印(项目名称.text)你知道吗


Tags: 文件代码文档网页by网站htmldriver
2条回答

为了给它一些时间来做事,我有几种方法来处理这个问题。你知道吗

一种是设置一个时间加载页面。你知道吗

driver = set_page_load_timeout(10) 

也许我也会用时间。睡眠来自时间模块的命令

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]"))

希望这有帮助!祝你好运

简单地说,看看加载你的chrome\u配置文件或你的浏览器配置文件的副本(无论你使用什么浏览器)不必重新登录后,第一次。你知道吗

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("user-data-dir=C:\\Path") #Path to your chrome profile
w = webdriver.Chrome(executable_path="C:\\Path\\To\\chromedriver.exe", chrome_options=options)

https://www.youtube.com/watch?v=9UCaZT48Nnw

相关问题 更多 >

    热门问题