Home > Blockchain >  implicitly_wait doesn't work python selenium
implicitly_wait doesn't work python selenium

Time:06-23

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

CodePudding user response:

I can try to use javascript.

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

CodePudding user response:

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)

CodePudding user response:

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