简体   繁体   English

Selenium 滚动到元素但没有点击

[英]Selenium scrolls to the element but does not click

Trying to click next button from navigation bar of website "https://uk.trustpilot.com/categories/bars_cafes?subcategories=cafe" using selenium in python.尝试使用 python 中的 selenium 从网站“https://uk.trustpilot.com/categories/bars_cafes?subcategories=cafe”的导航栏单击下一步按钮。

from selenium.webdriver import Chrome
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import time 

URL = "https://uk.trustpilot.com/categories/bars_cafes?subcategories=cafe"
driver = Chrome(ChromeDriverManager().install())


class Scraper:
    def __init__(self, website):
        self.website = website
    
    def get_website(self):
        return driver.get(self.website)

    def ignore_cookie(self):
        try:
            ignore_cookies = driver.find_element(by=By.XPATH, value='//*[@id="onetrust-reject-all- handler"]')
            ignore_cookies.click()
        except AttributeError:
            pass



    def next_page(self):
        driver.find_element(by=By.NAME, value="pagination-button-next").click()

The ignore cookie function works fine.忽略 cookie function 工作正常。 But next_page function scrolls to the next button but does not click it.但是 next_page function 滚动到下一个按钮但没有单击它。

Include the following imports:包括以下导入:

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

Edit your next_page function like so:像这样编辑您的next_page function:

wait = WebDriverWait(driver, 25)

next_page_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//a[@name="pagination-button-next"]')))
next_page_button.location_once_scrolled_into_view
t.sleep(2)
next_page_button.click()

See Selenium documentation at https://www.selenium.dev/documentation/请参阅 Selenium 文档,网址为https://www.selenium.dev/documentation/

This should do it:这应该这样做:

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = "https://uk.trustpilot.com/categories/bars_cafes?subcategories=cafe"

class Scraper:
    def __init__(self, website):
        self.driver = Chrome(ChromeDriverManager().install())
        self.driver.get(website)
        self.wait = WebDriverWait(self.driver,20)


    def ignore_cookie(self):
        self.driver.find_element(By.CSS_SELECTOR, "button[class^='onetrust-close-btn-handler']").click()


    def fetch_content(self):
        while True:
            for item in self.driver.find_elements(By.CSS_SELECTOR, "section > [class*='card_card']"):
                shop_name = item.find_element(By.CSS_SELECTOR, "a[name='business-unit-card'] p[class*='displayName']").text
                yield shop_name

            try:
                self.next_page()
                self.wait.until(EC.staleness_of(item))
            except Exception as err:
                self.driver.quit()
                return


    def next_page(self):
        next_page = self.driver.find_element(By.CSS_SELECTOR, "a[name='pagination-button-next']")
        self.driver.execute_script("arguments[0].click();", next_page)


scrape = Scraper(url)
scrape.ignore_cookie()
for title in scrape.fetch_content():
    print(title)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM