python 如何在selenium中为不同的div标签从左向右滚动页面

tsm1rwdh  于 2023-02-21  发布在  Python
关注(0)|答案(1)|浏览(138)

我试图刮所有的应用程序的网址从目标页面:-https://play.google.com/store/apps?device=使用下面的代码:-

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
import time
from tqdm import tqdm
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
driver = webdriver.Chrome(ChromeDriverManager().install(), options=options)
driver.maximize_window()

items = ['phone','tablet','tv','chromebook','watch','car']
target_url = "https://play.google.com/store/apps?device="

all_apps = []
for cat in tqdm(items):
    driver.get(target_url+cat)
    time.sleep(2)
    
    new_height = 0
    last_height = 0
    while True:
        # Scroll down to bottom
        driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
        # Wait to load page
        time.sleep(4)

        # Calculate new scroll height and compare with last scroll height
        new_height = driver.execute_script("return document.body.scrollHeight")

        # break condition
        if new_height == last_height:
            break
        last_height = new_height
        
    for i in driver.find_elements(By.XPATH,"//a[contains(@href,'/store/apps/details')]"):
        all_apps.append(i.get_attribute('href'))

上面的代码将页面上下滚动,并提供页面上所有可用应用的URL。

我尝试使用下面的代码单击该元素,但出现错误:
驱动程序.find_element(通过.XPATH,"//i[包含(文本(),'chevron_right')]”).单击()
错误:-

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

我尝试使用下面的代码:-

element = driver.find_element(By.XPATH,"//div[@class='bewvKb']") #any icon, may be that whatsapp icon here
hover = ActionChains(driver).move_to_element(element)
hover.perform()

element = driver.find_element(By.XPATH,"//i[text()='chevron_right']")
element.click()

没有选项点击突出显示的按钮,如图所示。有没有人能帮我这个像如何滚动页面侧身,使所有的内容可以从页面刮?

5sxhfpxr

5sxhfpxr1#

问题是,右箭头图标只有当你把鼠标悬停在一条线上的任何一个图标上时才会出现。所以,首先悬停在任何一个图标上,这样右箭头就会出现,然后发出点击,主要是这样的

element = driver.find_element_by_css_selector("#yDmH0d > c-wiz.SSPGKf.glB9Ve > div > div > div.N4FjMb.Z97G4e > c-wiz > div > c-wiz > c-wiz:nth-child(1) > c-wiz > section > div > div > div > div > div > div.aoJE7e.b0ZfVe > div:nth-child(1) > div > div > a > div.TjRVLb > img") #any icon, may be that whatsapp icon here
hover = ActionChains(driver).move_to_element(element)
hover.perform()

element = driver.find_element_by_xpath("//i[text()='chevron_right']")
element.click()

相关问题