简体   繁体   English

Selenium Webdriver Python 没有循环

[英]Selenium Webdriver Python is not looping

I'm trying to use Selenium to click on multiple links one each a time to download multiple CSV files, the problem here is the selenium makes the donwload of about few csv files but in the middle of the loop it stops working, crashes the Browser accusin that don't have internet and close the driver.我正在尝试使用 Selenium 一次单击多个链接以下载多个 CSV 文件,这里的问题是 selenium 使下载了大约几个 csv 文件但在循环中间它停止工作,使浏览器崩溃指责没有互联网并关闭驱动程序。 I already put the chromedriver.exe in the same folder and put the path but it still not working.我已经将 chromedriver.exe 放在同一个文件夹中并放置了路径,但它仍然无法正常工作。

from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time
import os
import re

new_directory = r"Z:\BI_Database_teste"
for document in os.listdir(new_directory):
    os.remove(os.path.join(new_directory, document))

url = 'myPersonalURL'

chrome_options = Options()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome('chromedriver.exe', options=chrome_options)
params = {'behavior': 'allow', 'downloadPath': new_directory}
browser.execute_cdp_cmd('Page.setDownloadBehavior', params)
browser.get(url)

time.sleep(2)


input_email = browser.find_element(By.ID, 'email')
input_email.send_keys('myEmail')
input_password = browser.find_element(By.ID, 'password')
input_password.send_keys('myPassword')
input_password.submit()

input_question = browser.find_element(By.XPATH, '/html/body/div[2]/div[2]/form/table/tbody/tr[3]/td/table/tbody/tr[2]/td[2]').text
answer_field = browser.find_element(By.XPATH, '/html/body/div[2]/div[2]/form/table/tbody/tr[3]/td/table/tbody/tr[3]/td[2]/input')

if input_question == 'question':
    answer_field.send_keys('answer')
elif input_question == 'question':
    answer_field.send_keys('answer')
else:
    answer_field.send_keys('answer')

time.sleep(2)

answer_field.submit()
time.sleep(4)


links = browser.find_elements(By.LINK_TEXT, 'Export (CSV)')
links_len = len(links)
print(str(links_len) + ' BI Databases to Download')
list_count = 0

for link in range(list_count, links_len):
    time.sleep(2)
    links[list_count].click()
    list_count = list_count + 1
    print(str(list_count) + ' BI Databases downloaded')

browser.quit()


for file in os.listdir(new_directory):
    if file.startswith("PBI"):
        try:
            os.rename(os.path.join(new_directory, file), os.path.join(new_directory, re.sub('[0-9]', '', file)))
        except:
            pass

print('BI Databases Download Successfully!')```


Could someone help me to find out why the webdriver stops working in the middle of the loop?

If you think that for some reason the driver isn't loaded correctly then you can download the webdriver on runtime from your code, this might help,如果您认为由于某种原因驱动程序未正确加载,那么您可以在运行时从您的代码中下载 webdriver,这可能会有所帮助,

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

chrome = webdriver.Chrome(ChromeDriverManager().install(), options=options)

without much further context, the question cannot be answered or reproduced.在没有更多背景的情况下,无法回答或复制该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM