Selenium in Python - Implicitly wait and explicitly wait

0

This happens to me with this function, if I use a time.sllep () it works correctly but from what I understand it is quite inefficient to use it.

def visible_en_tienda(driver): 
 time.sleep(2)

 xpath = "/html/body/div[1]/main/div[2]/section/div[5]/div/div/label/span"  
 visible = driver.find_element_by_xpath(xpath) 
 visible.click() 

 xpath = "/html/body/div[1]/main/div[2]/section/div[13]/div[2]/div/div/div[1]/div[1]/div/div/label/span"
 visible = driver.find_element_by_xpath(xpath) 
 visible.click()

Then I tried to use the implicitly_wait () but it is as if it did not exist

def visible_en_tienda(driver):

 driver.implicitly_wait(10)
 xpath = "/html/body/div[1]/main/div[2]/section/div[5]/div/div/label/span" 
 visible = driver.find_element_by_xpath(xpath) 
 visible.click() 

 driver.implicitly_wait(10) 
 xpath = "/html/body/div[1]/main/div[2]/section/div[13]/div[2]/div/div/div[1]/div[1]/div/div/label/span"
 visible = driver.find_element_by_xpath(xpath)
 visible.click()

And I get this error, apparently there is another element on top of loading ...

selenium.common.exceptions.ElementClickInterceptedException: 
Message: Element <span class="control__indicator"> is not clickable 
at point (374,322.5) because another element <div class="bg"> 
obscures it

So I try to use the explicit wait in this way but it still gives me the same error.

def visible_en_tienda(driver):
 wait = WebDriverWait(driver, 10)     

 xpath = "/html/body/div[1]/main/div[2]/section/div[5]/div/div/label/span" 
 visible = wait.until(EC.element_to_be_clickable((By.XPATH, xpath))) 
 visible.click() 

 xpath = "/html/body/div[1]/main/div[2]/section/div[13]/div[2]/div/div/div[1]/div[1]/div/div/label/span"
 visible = wait.until(EC.element_to_be_clickable((By.XPATH, xpath)))
 visible.click()

Then I'm not sure if the waits do not work or I'm using them badly. And if I'm using them badly, what am I doing wrong?

    
asked by Iván Rodríguez 09.07.2018 в 19:09
source

1 answer

1

What I use to avoid explicit waiting, is this (although it is Java code, in Python it will be practically the same):

new FluentWait<WebDriver>(driver).withTimeout(IMPLICIT_TIMEOUT, TimeUnit.SECONDS)
            .pollingEvery(RETRY_TIME, TimeUnit.SECONDS)
            .ignoring(NoSuchElementException.class)
            .until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));

Where:

  • IMPLICIT_TIMEOUT is the maximum value, in seconds, that the driver will wait for the element
  • RETRY_TIME how often I look for the element again.
  • xpath the Xpath of the object to search.
  • This returns a WebElement with which you can interact, either to make a click or to send text.

        
    answered by 10.07.2018 в 08:22