Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何将等待添加到Selenium scraping程序中?_Python 3.x_Selenium Webdriver_Anaconda_Selenium Chromedriver - Fatal编程技术网

Python 3.x 如何将等待添加到Selenium scraping程序中?

Python 3.x 如何将等待添加到Selenium scraping程序中?,python-3.x,selenium-webdriver,anaconda,selenium-chromedriver,Python 3.x,Selenium Webdriver,Anaconda,Selenium Chromedriver,我正在尝试使用Python和Selenium来抓取一个网站并保存信息。scrape很简单,只需要在两个下拉菜单中选择state和district,单击submit按钮,然后将表读写到csv 我确信我的软件包安装正确,我的程序甚至可以运行,但只是在某些时候。我的猜测是,如果没有正确的Selenium驱动程序“等待”,我的程序就会崩溃,因为它找不到正确的css_选择器。我将在下面发布程序,如果有人对如何正确合并Selenium驱动程序“等待”有任何建议,我将非常感谢您的帮助 非常感谢,以下是节目:

我正在尝试使用Python和Selenium来抓取一个网站并保存信息。scrape很简单,只需要在两个下拉菜单中选择state和district,单击submit按钮,然后将表读写到csv

我确信我的软件包安装正确,我的程序甚至可以运行,但只是在某些时候。我的猜测是,如果没有正确的Selenium驱动程序“等待”,我的程序就会崩溃,因为它找不到正确的css_选择器。我将在下面发布程序,如果有人对如何正确合并Selenium驱动程序“等待”有任何建议,我将非常感谢您的帮助

非常感谢,以下是节目:

import time
import re
import string
import urllib.parse
import pandas
import numpy
import os
import csv

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup

driver = webdriver.Chrome('C:/chromedriver_win32/chromedriver.exe')
url = "https://myhpgas.in/myHPGas/HPGas/LocateDistributor.aspx"
driver.set_window_size(1120, 550)
driver.get(url);
time.sleep(5) 
stateList = driver.find_element_by_css_selector("#ContentPlaceHolder1_ddlState")
options = stateList.find_elements_by_tag_name("option")

optionsList = []

for option in options:
    optionsList.append(option.get_attribute("value"))

optionsList[1:len(optionsList)]
for optionValue in optionsList:

    select = Select(driver.find_element_by_css_selector("#ContentPlaceHolder1_ddlState"))
    select.select_by_value(optionValue)
    districtList = driver.find_element_by_css_selector("#ContentPlaceHolder1_ddlDistrict")
    distOptions = districtList.find_elements_by_tag_name("option")

    distOptionsList = []

    for distOption in distOptions: #iterate over the options, place attribute value in list
        distOptionsList.append(distOption.get_attribute("value"))

    for distOptionValue in distOptionsList[1:len(distOptionsList)]:

        distSelect = Select(driver.find_element_by_css_selector("#ContentPlaceHolder1_ddlDistrict"))
        distSelect.select_by_value(distOptionValue)
        driver.find_element_by_css_selector('#ContentPlaceHolder1_btnShowList').click()
        data = []                      
        for tr in driver.find_elements_by_css_selector('#ContentPlaceHolder1_gvDistributor'):
            tds = tr.find_elements_by_tag_name('td')
            if tds: 
                data.append([td.text for td in tds])
        print(data)
        dataRows = int(numpy.array(data).size / 7)
        rowsTimesColumns = (dataRows * 7) -1
        newArray = numpy.array(data)
        outArray = newArray[0:rowsTimesColumns]
        test = pandas.DataFrame(outArray.reshape(dataRows,7), columns=['no', 'distributor', 'address','contact1', 'contact2', 'contact3', 'map'])

        file_path = 'Users/outpath' + '_' + optionValue + '_' + distOptionValue + '.csv'
        test.to_csv(file_path, sep=',')
        driver.back()
    driver.back()

你能告诉我哪一行返回错误吗!?还可以使用XPath

我看不到实现显式等待的语句

WebDriverWait(driver, 30).until(EC.presence_of_element_located_by(By.CSS_SELECTOR,*your css selector*))

你能告诉我哪一行返回错误吗!?还可以使用XPath

我看不到实现显式等待的语句

WebDriverWait(driver, 30).until(EC.presence_of_element_located_by(By.CSS_SELECTOR,*your css selector*))

非常感谢,悉达思!我发现了错误的来源,它涉及到没有在第30行添加optionsList=optionsList[1:len(optionsList)]。您是对的,没有实现显式等待的语句。但是,我将实现您建议的代码行。另外,为什么建议使用XPath而不是CSS选择器?如果你认为XPath更优秀,我会做出改变。最好的,DaveWell使用XPath这更像是一种习惯。我个人觉得它们更容易实现和定位。事实上,css选择器更快,归结为个人偏好。非常感谢,Siddharth!我发现了错误的来源,它涉及到没有在第30行添加optionsList=optionsList[1:len(optionsList)]。您是对的,没有实现显式等待的语句。但是,我将实现您建议的代码行。另外,为什么建议使用XPath而不是CSS选择器?如果你认为XPath更优秀,我会做出改变。最好的,DaveWell使用XPath这更像是一种习惯。我个人觉得它们更容易实现和定位。事实上,css选择器更快,归结为个人偏好。