如何等待页面加载,然后按硒中的加载更多按钮?

时间:2019-07-16 06:35:55

标签: python html selenium selenium-webdriver web-scraping

使用硒进行加载和分页,需要单击“加载更多”按钮,但是单击后加载超过100次

我正在拦截此元素点击错误。因为在100次浏览后页面需要花费一些时间来加载。和代码不知道在哪里单击。

还尝试将睡眠时间增加到20秒,但是在某些情况下,如果页面花费了20秒以上,则代码返回错误

错误:


ElementNotInteractableException: Message: element not interactable (Session info: chrome=75.0.3770.100)


代码:

from selenium import webdriver
import time
import pandas as pd
driver = webdriver.Chrome('/Users/1/chromedriver.exe')
driver.get('https://simpletire.com/catalog?select=1&brand=61&query=catalog')

click_more=True
while click_more:
    time.sleep(2)

    driver.find_element_by_css_selector(".btn.btn-primary.btn-lg").click();

4 个答案:

答案 0 :(得分:1)

考虑引入Explicit Wait来确保在尝试单击按钮之前按钮在那里

示例代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('/Users/1/chromedriver.exe')
driver.get('https://simpletire.com/catalog?select=1&brand=61&query=catalog')

click_more=True
while click_more:
    WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".btn.btn-primary.btn-lg"))).click()

更多信息:

答案 1 :(得分:1)

在这种情况下,我通常会停止页面加载,也许滚动几次,然后将屏幕坐标与诸如AppRobotic之类的宏工具一起使用以单击按钮。这样的事情应该适合您:

import win32com.client
from win32com.client import Dispatch
x = win32com.client.Dispatch("AppRobotic.API")
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome('/Users/1/chromedriver.exe')
driver.get('https://simpletire.com/catalog?select=1&brand=61&query=catalog')
# wait 20 seconds per question
x.Wait(20000)
# scroll down a couple of times in case page javascript is waiting for user interaction
x.Type("{DOWN}")
x.Wait(2000)
x.Type("{DOWN}")
x.Wait(2000)
# forcefully stop pageload at this point
driver.execute_script("window.stop();")
# if clicking with Selenium still does not work here, use screen coordinates
x.MoveCursor(xCoord, yCoord)
x.MouseLeftClick
x.Wait(2000)

答案 2 :(得分:0)

似乎您可能正在页面底部浏览google广告。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException

driver = webdriver.Chrome()
driver.get('https://simpletire.com/catalog?select=1&brand=61&query=catalog')

click_more=True
wait = WebDriverWait(driver, 30)
while click_more:
    try:
        elem = wait.until(EC.visibility_of_element_located((By.XPATH, '//button[@class="btn btn-primary btn-lg"]')), 
                          "Cannot find 'Load More' button")
        elem.click()
    except TimeoutException:
        click_more = False

使用xpath和visibility_of_element_located出现以下异常:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button class="btn btn-primary btn-lg">...</button> is not clickable at point (591, 797). Other element would receive the click: <iframe id="google_ads_iframe_/21692090825/ST_FlexFooter_0" title="3rd party ad content" name="google_ads_iframe_/21692090825/ST_FlexFooter_0" width="970" height="90" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" srcdoc="" style="border: 0px; vertical-align: bottom;" data-google-container-id="1" data-load-complete="true"></iframe>

您需要先关闭Google添加或向下滚动页面,然后再尝试单击按钮。一旦完成,while循环就应该起作用。

答案 3 :(得分:0)

尝试下面的脚本以继续单击该按钮,直到没有可单击的为止。

|