无法滚动元素以查看和单击。Python Selenium

阿比舍克赖

我正在尝试从此页面获取插件。如果该项目是可定制的

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

收集完所有内容后divs,我尝试转到页面顶部(不起作用)。我检查是否存在可定制 div的。如果是这样,将其滚动到视图中,单击“添加”按钮并获取“添加详细信息”。我试过了,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)
马可

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

无法单击元素,Selenium Python

无法单击元素Python Selenium

无法单击元素-Selenium python

使用Selenium和Python无法定位和单击元素

selenium.common.exceptions.ElementClickInterceptedException:消息:拦截了元素单击:Selenium和Python无法单击元素

Python 和 Selenium:无法找到要单击的特定 (href) 元素

似乎无法使用Selenium和python单击元素

Selenium和Chromedriver-无法单击元素

ElementClickInterceptedException:消息:拦截了元素单击:Selenium和Python无法单击元素<label>

Python Selenium错误元素此时无法单击

无法使用Python Selenium单击元素

Python + Selenium-无法单击元素

Python / Selenium-类元素无法单击

使用 Selenium 和 Python 访问网页时,Selenium 无法单击元素

无法使用python和selenium单击链接

Selenium WebDriver单击输入元素以上传冻结的文件

如何向上滚动,然后使用Selenium和python单击

Selenium无法单击ElementClickInterceptedException Selenium按钮Python

Python Selenium:如何单击元素

无法定位元素:通过 Selenium 和 Python 单击按钮时

NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法定位元素

Selenium python错误:元素无法滚动到视图中

Python Selenium选择:“元素<option>无法滚动到视图中”

无法向下滚动到 Selenium 中的元素,python

selenium.common.exceptions.WebDriverException:消息:尝试使用 Selenium 和 Python 单击元素时无法将数据转换为对象

无法单击按钮 Selenium Python

Selenium Python:无法单击按钮

Python:Selenium无法单击XPath文本元素

如何使用Selenium和Python单击<svg:image>元素