如何使用Selenium Python动态点击加载按钮?

3
我想点击load-more,直到它从页面上消失。
我尝试过,但有时候会起作用,有时候会出现错误。这不是我采取的完美解决方法。
我可以在一个列表中拥有多个URL,一个一个地点击并load-more,直到它从页面上消失。
谢谢您的帮助。 代码
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: ''

如果您切换到Chrome会发生什么?问题是否仍然可以重现?谢谢。 - alecxe
你使用的是哪个版本的Selenium和Firefox? - alecxe
我正在使用Selenium 2.45.0版本和Firefox 37.0.1版本。 - Mukesh
好的,有两件事可以尝试:使用Chrome浏览器和降级到Firefox 35.0.1版本。 - alecxe
在使用selenium 2.45.0和firefox 35.0.1或phantomjs时,出现错误Error XPATH= LOAD MORE 19 XPATH= LOAD MORE 14 XPATH= LOAD MORE 9 XPATH= LOAD MORE 4 raise exception_class(message, screen, stacktrace) selenium.common.exceptions.StaleElementReferenceException: Message: Element is no longer attached to the DOM Stacktrace: at fxdriver.cache.getElementAt (resource://fxdriver/modules/web-element-cache.js:8956) - Mukesh
错误:StaleElementReferenceException: Message: Element is no longer attached to the DOM Stacktrace: 在使用selenium 2.45.0和firefox 35.0.1时出现。你能帮我解决一下吗? - Mukesh
1个回答

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接