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

标签 python firefox selenium selenium-webdriver

我想点击 load-more 直到它在该页面上消失。

我已经尝试过了,但有时它会工作或出错。这不是我所做的完美解决方案。

我可以列表中有多个 url 并逐一点击并加载更多 直到它从该页面消失。

在此先感谢您的帮助。

代码

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: ''

最佳答案

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

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

关于python - 如何使用 selenium python 动态单击加载按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30490549/

相关文章:

java - 如何处理 <div id ="preloader"> 用 Selenium 模糊其他元素,检查其他答案没有任何帮助

Python Selenium 按向下箭头显示所有页面内容

python - 这个 ZPT 模板有什么问题?

javascript - 如何在 Firefox 插件上使用 jQuery 1.5.2+?

python - 画一条几乎直线

css - 当在没有宽度的 float div 中 float 子项时,Chrome 和 Firefox 的渲染不同。

firefox - 阻止 Firefox 缓存本地主机?

java - 在 Selenium WebDriver 中打开新选项卡后,如何使其成为浏览器中的可见/Activity 选项卡?

python - 在文件中创建工作数据结构

python - 在csv excel文件中对相关数据进行分组