如何点击加载按钮动态使用硒python? [英] How to click on load button dynamically using selenium python?

查看:210
本文介绍了如何点击加载按钮动态使用硒python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我可以在列表中多个网址,然后逐一打开, load-



感谢您的帮助。 > Code

  driver = webdriver.Firefox()
$ b $ url = [https ://www.zomato.com/HauzKhasSocial,https://www.zomato.com/ncr/wendys-sector-29-gurgaon,https://www.zomato.com/vaultcafecp]
加载url:
driver.get(load)
xpath_content ='// div [@class =load-more]'
temp_xpath =true
而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
printXPATH =,temp_xpath
点击();')除了TimeoutException外,

打印驱动程序.title,no xpath of pagination
temp_xpath =
continue

大多数时候我在运行程序的时候出现错误。

 文件/usr/local/lib/python2.7/ dist-packages / selenium / webdriver / remote / webdriver.py,第173行,执行
response = self.command_executor.execute(driver_command,params)
文件/ usr / local / lib / python2在执行
返回self._request(command_info [0],url,body = data)
中的.7 / dist-packages / selenium / webdriver / remote / remote_connection.py e/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py,第380行,在_request
resp = self._conn.getresponse()
文件/usr/lib/python2.7/httplib.py,第1045行,在getresponse
response.begin()
文件/usr/lib/python2.7/httplib.py,第409行开头
version,status,reason = self._read_status()
文件/usr/lib/python2.7/httplib.py,第373行,在_read_status中
raise BadStatusLine(line)
httplib.BadStatusLine:''


解决方案

您可能会因为在最新版本的Selenium webdrivers中修复的错误而导致BadStatus错误。最近我遇到了类似的情况,这里是与帮助我的开发者进行讨论的线索。

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


I want to click on load-more until it disappear on that page.

I have tried but it sometimes work or giving error. It is not perfect solution which i did.

I can have multiple url in a list and hit one by one and load-more until it disappear from that page.

Thanks in advance for helping.

Code

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

Most of time I get following error while running my program.

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

解决方案

You probably get the BadStatus error because of a bug that has been fixed in the latest versions of Selenium webdrivers. I got into a similar situation recently and here's the thread of discussion with the developers that helped me out.

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

这篇关于如何点击加载按钮动态使用硒python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆