如何使用selenium python动态点击加载按钮?

时间:2015-05-27 18:37:48

标签: python firefox selenium selenium-webdriver

我想点击加载更多,直到它在该页面上消失。

我已经尝试但有时会工作或给出错误。这不是我做的完美解决方案。

我可以在列表中多个网址并逐个点击加载更多,直到它从该页面消失。

提前感谢您的帮助。

代码

driver = webdriver.Firefox()

url = ["https://www.zomato.com/HauzKhasSocial","https://www.zomato.com/ncr/wendys-sector-29-gurgaon","https://www.zomato.com/vaultcafecp"]
for load in url:
    driver.get(load)
    xpath_content='//div[@class = "load-more"]' 
    temp_xpath="true"
    while temp_xpath: 
        try:  
            #driver.implicitly.wait(15)
            #WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH,xpath_content))) 
            WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH,xpath_content)))
            #urls=driver.find_element_by_xpath(xpath_content) 
            urls=driver.find_element_by_xpath(xpath_content)
            text=urls.text
            if text:
                temp_xpath=text  
            print "XPATH=",temp_xpath  
            driver.find_element_by_xpath(xpath_content).click()
            #driver.execute_script('$("div.load-more").click();')  
        except TimeoutException:
            print driver.title, "no xpath of pagination"
            temp_xpath=""
            continue

大多数时候,我在运行程序时遇到以下错误。

 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 173, in execute
response = self.command_executor.execute(driver_command, params)
 File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 349, in execute
return self._request(command_info[0], url, body=data)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 380, in _request
resp = self._conn.getresponse()
File "/usr/lib/python2.7/httplib.py", line 1045, in getresponse
response.begin()
File "/usr/lib/python2.7/httplib.py", line 409, in begin
version, status, reason = self._read_status()
File "/usr/lib/python2.7/httplib.py", line 373, in _read_status
raise BadStatusLine(line)
httplib.BadStatusLine: ''

1 个答案:

答案 0 :(得分:0)

由于最新版本的Selenium webdrivers中已修复的错误,您可能会收到BadStatus错误。我最近遇到了类似的情况,这是与帮助我的开发人员讨论的主题。

https://bugs.chromium.org/p/chromedriver/issues/detail?id=1548