无法滚动元素以查看并单击。Python硒

2024-09-28 03:13:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试从该页面获取附加组件。如果项目可定制

https://www.swiggy.com/restaurants/imperial-restaurant-shivajinagar-central-bangalore-bangalore-48556

在收集完所有的divs之后,我尝试转到页面顶部(不起作用)。我检查可定制的div是否存在。如果是这样,请滚动到视图中,单击Add按钮并获取Add-on详细信息。我试过了,actionChains,没用execute_script也不做任何事情。以前的点击是有效的,但是页面上下滚动有点搞砸了。几个小时来,我一直在努力把这件事做好。非常感谢您的帮助

    for i in range(6):
        item_dvs = driver.find_elements_by_class_name('_2wg_t')
        driver.execute_script("window.scrollBy(0, 3100)")
    driver.execute_script("window.scrollBy(0,-18500);")
    for div in item_dvs:
        try:
            name = div.find_element_by_class_name('styles_itemNameText__3bcKX')
            price = div.find_element_by_class_name('rupee')
            if div.find_elements_by_class_name('styles_itemDesc__MTsVd'):
                desc = div.find_element_by_class_name('styles_itemDesc__MTsVd').text
            else:
                desc = None
            is_cust = div.find_element_by_class_name('_1C1Fl _23qjy')
            if is_cust:
                driver.execute_script("arguments[0].scrollIntoView();", is_cust)

Tags: namedivaddexecutebyisdriverscript
1条回答
网友
1楼 · 发布于 2024-09-28 03:13:16

首先,我建议您使用WebDriverWait,因为当元素不存在时,行if div.find_elements_by_class_name('styles_itemDesc__MTsVd')is_cust = div.find_element_by_class_name('_1C1Fl _23qjy')将引发异常。使用WebDriverWait的另一个原因是等待Add按钮可单击。这里是两个问题的解决方案,您可以使用try/catch来处理描述和“customizable”元素的存在,并且可以等待Add按钮可单击:

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 import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.common.exceptions import TimeoutException


driver = webdriver.Chrome()
driver.get("https://www.swiggy.com/restaurants/imperial-restaurant-shivajinagar-central-bangalore-bangalore-48556")
wait = WebDriverWait(driver, 10)

try:
    item_dvs = []
    for i in range(6):
        item_dvs = wait.until(ec.visibility_of_all_elements_located((By.CLASS_NAME, '_2wg_t')))
        driver.execute_script("window.scrollBy(0, 3100)")
    driver.execute_script("window.scrollBy(0,-18500);")

    for div in item_dvs:
        wait_div = WebDriverWait(div, 10)
        name = wait_div.until(ec.visibility_of_element_located((By.CLASS_NAME, 'styles_itemNameText__3bcKX')))
        price = wait_div.until(ec.visibility_of_element_located((By.CLASS_NAME, 'rupee')))

        try:
            desc = wait_div.until(ec.visibility_of_element_located((By.XPATH, ".//div[contains(@class, 'styles_itemDesc__MTsVd')]"))).text
        except TimeoutException:
            desc = None

        try:
            is_cust = wait_div.until(ec.visibility_of_element_located((By.XPATH, ".//div[contains(@class, '_1C1Fl _23qjy')]")))
            add_on = wait_div.until(ec.element_to_be_clickable((By.XPATH, ".//div[contains(@class, 'F8dpS _3L1X9')]")))
            ActionChains(driver).move_to_element(add_on).click().perform()
            # get add on details
            # close add window
        except TimeoutException:
            pass

    driver.close()

except Exception as e:
    driver.close()
    raise e

相关问题 更多 >

    热门问题