0

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
1
  • just a note that all waits use time.sleep inside their polling loops. There's nothing bad about time.sleep. Commented Jun 22, 2022 at 18:33

4 Answers 4

1

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()
Sign up to request clarification or add additional context in comments.

1 Comment

suddenly it doesn't work
0

I can try to use javascript.

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

2 Comments

Thanks for answer, it works in browser console, but nothnig happend in python
I could try, with driver.execute_script("arguments[0].click();", element) It's other way
0

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)

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.