我正在尝试导航到多个页面并从每个页面下载PDF。这些页面由同一站点托管,只是URL不同,因此要下载的xpath都相同。我拥有的当前代码有效,但是我是Python初学者,我敢肯定有一种更简单的方法来合并它-只是很难弄清楚如何做到!
我尝试使用for循环,但出现一些错误。
这就是我现在正在使用的:
one ='urlone'
two = 'urltwo'
three = 'urlthree'
try:
download = "C:/pathtodownload"
driver = webdriver.Chrome('driver.exe', chrome_options=options)
driver.get(one)
driver.maximize_window()
actions = driver.find_element_by_xpath('xpathhere')
actions.click()
time.sleep(4)
driver.get(two)
driver.maximize_window()
actions = driver.find_element_by_xpath('xpathhere')
actions.click()
time.sleep(4)
driver.get(three)
driver.maximize_window()
actions = driver.find_element_by_xpath('xpathhere')
actions.click()
time.sleep(4)
finally:
driver.quit
我相信我应该可以执行类似的操作,但是我收到了错误消息。 WebDriverException:消息:未知错误:“ url”必须为字符串
urls = ['urlone', 'urltwo', 'urlthree']
try:
download = "C:/pathtodownload"
driver = webdriver.Chrome('driver.exe', chrome_options=options)
for url in urls:
driver.get(urls)
driver.maximize_window()
actions = driver.find_element_by_xpath('xpathhere')
actions.click()
time.sleep(4)
答案 0 :(得分:1)
我发现了错误。
urls = ['urlone', 'urltwo', 'urlthree']
try:
download = "C:/pathtodownload"
driver = webdriver.Chrome('driver.exe', chrome_options=options)
for url in urls:
driver.get(url) # Here I changed urls that you used in your code to url
driver.maximize_window()
actions = driver.find_element_by_xpath('xpathhere')
actions.click()
time.sleep(4)
基本上在注释行中使用url,您试图在仅接受字符串的.get()方法中提供整个url列表。