首页 > 解决方案 > 硒与python如何点击弹出按钮

问题描述

我已经尝试了许多可能的情况,但我无法访问它请帮助我解决这个问题,我如何点击 common-home res layout-home1 加载的隐藏滚动模态 - 在此处打开我想点击(添加到购物车)和(愿望清单)我的代码如下

    from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver import ActionChains
from time import sleep
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import ElementNotVisibleException


driver = webdriver.Chrome(executable_path='C:\Program Files\JetBrains\PyCharm 2021.1.1/chromedriver.exe')


driver.maximize_window()
driver.get("http://esindbaad.com/")
#mouse hour to catagories
beauty = driver.find_element_by_xpath("//*[@id='head-wrapper']/div/div/div/div/ul/li[3]/a")
body = driver.find_element_by_xpath("//*[@id='head-wrapper']/div/div/div/div/ul/li[3]/div/div/div/div/div/div[1]/div[3]/div/ul/li[6]/a")
actions = ActionChains(driver)
actions.move_to_element(beauty).move_to_element(body).click().perform()
#add to wishlist

wish = driver.find_element_by_xpath("//*[@id='content']/div/div[1]/div[1]/div/div[1]/div/button[1]/i")
actions = ActionChains(driver)
actions.move_to_element(wish).click().perform()
#click and view product
product = driver.find_element_by_xpath("//*[@id='content']/div/div[1]/div[1]/div/div[1]/div/a")
actions = ActionChains(driver)
actions.move_to_element(product).click().perform()
# product added to cart button click
driver.switch_to.frame(iframe)
addcart = driver.find_element_by_id("button-cart")
actions = ActionChains(driver)
actions.move_to_element(addcart).click().perform()

wishlist = driver.find_element_by_xpath("//*[@id='product']/div[2]/div[3]/ul/li/a")
actions = ActionChains(driver)
actions.move_to_element(wishlist).click().perform()




driver.execute_script("window.scrollBy(0,document.body.scrollHeight)")

标签: pythonseleniumautomationscreen-scraping

解决方案


等待窗口出现,然后使用显式等待单击它

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

addcart = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.ID, "button-cart")))

推荐阅读