我有一个充满 URL 的文件(大约 3000 个 URL),要求是在浏览器选项卡中调用每个 URL,然后单击每个页面(在加载的页面上单击就足够了),它重定向到不同的页面。这就是我需要做的。
我已经为 url 编写了一个代码来逐个检索并在页面上点击(页面旨在点击任何地方登陆或重定向到不同的页面)。但是,在运行代码时会抛出“无效参数”错误。任何想法,我该如何纠正错误?
感谢您的支持。
with open(dataFile, 'r') as urlFile:
urlFile.readline()
for url in urlFile:
driver = webdriver.Chrome('C:\\chromedriver_win32\\chromedriver.exe')
driver.get(url)
link = driver.find_element_by_xpath("/html/body")
link.click()
urlFile.close()
上面的代码应该在新的浏览器选项卡上从文件中提取每个 url,然后单击。重复相同的操作,直到完成文件中的所有 url。
请您参考如下方法:
我做了这样的事情 代码
from selenium import webdriver
import time
driver = webdriver.Chrome('driver_location')
f = open("dataFile.txt", 'r')
for line in f:
driver.get(line)
link = driver.find_element_by_css_selector("#header > div:nth-child(3) > div > div > div:nth-child(3) > div > a")
link.click()
print(driver.current_url)
f.close()
dataFile.txt 文件中的 url
http://automationpractice.com/index.php
http://automationpractice.com/index.php
http://automationpractice.com/index.php
输出
http://automationpractice.com/index.php?controller=order
http://automationpractice.com/index.php?controller=order
http://automationpractice.com/index.php?controller=order
希望这对您有所帮助。
