Inditly_wait不做工作python selenium

发布于 2025-02-09 20:00:29 字数 1019 浏览 2 评论 0原文

我有问题 - 使用隐式_WAIT时,单击“添加到袋子”的按钮不会发生。如果我使用time.sleep,一切都可以,但是时间。 那么,我需要什么?获取URL,单击设备,单击“添加到袋子”按钮。 是的,我知道 https://selenium-python.readthedthedocs.io/waits.io/waits.html 和,但这对我没有帮助。请帮我)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import WebDriverException
import time
try:
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://www.oumua.me/shop")# Get URL
    browser.implicitly_wait(10)
    browser.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div/div[1]/div[1]').click() #Click on device button
    browser.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[1]').click() #Click Add to the bag that doesn't working

i got problem - click on button 'add to the bag' doesn't happen when using implicitly_wait. If i use time.sleep, all works ok, but time.sleep is bad method.
So, what i need? Get URl, click on device, click on 'add to the bag' button.
Yes, i know about https://selenium-python.readthedocs.io/waits.html and https://www.selenium.dev/documentation/webdriver/waits/ but it not help for me. Please help me)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import WebDriverException
import time
try:
    browser = webdriver.Chrome()
    browser.maximize_window()
    browser.get("https://www.oumua.me/shop")# Get URL
    browser.implicitly_wait(10)
    browser.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div/div[1]/div[1]').click() #Click on device button
    browser.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[1]').click() #Click Add to the bag that doesn't working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

£噩梦荏苒 2025-02-16 20:00:29

也许等待它可单击?

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

element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "'/html/body/div[1]/div[2]/div[3]/div[1]")))

element.click()

Maybe wait for it to be clickable?

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

element = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "'/html/body/div[1]/div[2]/div[3]/div[1]")))

element.click()
丑丑阿 2025-02-16 20:00:29

我可以尝试使用JavaScript。

js = 'document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();'
driver.execute_script(js)

I can try to use javascript.

js = 'document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();'
driver.execute_script(js)
喵星人汪星人 2025-02-16 20:00:29

我检查一下,它对我有用,而无需添加隐式explicit等待,以防万一您不想使用Waits,您可以尝试下面点击此按钮的方式下方

element = driver.findElement(By.CSS_SELECTOR(".styles__Button-sc-1fxagfa-17.styles__AddButton-sc-1fxagfa-18.bZGRJp.TZGpT"))

driver.execute_script("arguments[0].click();", element)

I check and it's working for me without adding the implicit or explicit wait, in case you do not want to use waits, you can try the below way to click on this button

element = driver.findElement(By.CSS_SELECTOR(".styles__Button-sc-1fxagfa-17.styles__AddButton-sc-1fxagfa-18.bZGRJp.TZGpT"))

driver.execute_script("arguments[0].click();", element)
坏尐絯 2025-02-16 20:00:29

我也试图运行它,对我不起作用。看来browser.page_source没有时间更新。我认为最好像@data_sc一样使用显式等待。

PS在这里有效,但这不是最好的解决方案:

from time import sleep

driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div/div[1]/div[1]').click()
sleep(1)
driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[1]').click()

I tried to run it too, it doesn't work for me. It seems that somehow the browser.page_source does not have time to update. I think it's better to use explicit waits as @data_sc did.

P.S. Here it works, but it's not the best solution:

from time import sleep

driver.find_element(By.XPATH, '//*[@id="__next"]/div[2]/div/div[1]/div[1]').click()
sleep(1)
driver.find_element(By.XPATH, '/html/body/div[1]/div[2]/div[3]/div[1]').click()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文