Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in DevOps and Agile by (19.7k points)
edited by

I have been tasked with writing a parser to click a button on a website and I am having issues to click only one of the buttons. The following code works on every button except one.

Here's the HTML: http://pastebin.com/6dLF5ru8

here's the source HTML: http://pastebin.com/XhsedGLb

python code:

 driver = webdriver.Firefox()  

 ...

 el = driver.find_element_by_id("-spel-nba")

 actions.move_to_element(el)

 actions.sleep(.1)

 actions.click()

 actions.perform()

I am getting this error.

ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

as per Saifur I just tried waits with the same element not visible exception:

wait = WebDriverWait(driver, 10)

wait.until(EC.presence_of_element_located((By.XPATH, "//input[contains(@id,'spsel')][@value='nba']"))).click()

closed

2 Answers

+1 vote
by (62.9k points)
selected by
 
Best answer

When you do the stated flow, when you want to click on the Logout button, you might get the above exception as the button is overlapped by the toaster message. Webdriver will not be able to click on the button and throws a perfect exception stating other elements would receive the click.

Solution:

You need to wait until the toaster message disappears. And we know how to wait in selenium.

To solve the above issue, You must use the wait provided by selenium. Now you will ask which wait we should use?

  • Thread.sleep(value): It is not advisable. You never know how much time you need to make web driver sleep. It might increase your test execution time drastically and would not serve the purpose always.

  • Implicit wait: This will not work always. The implicit wait will make the web driver wait until it does not find it. Here the issue is that webdriver finds web elements but could not click on it or element is not visible. The implicit wait can not stop webdriver once web element is found. This wait does not bother if the element is displayed or not.

  • Explicit wait: This is called dynamic wait and we should use it. ExpectedConditions class provides so many conditions until you want to wait. We have a condition which we can use to wait until the element is displayed. It will make web driver wait till the element is displayed on UI, not till the time element is found.

Sample code:

      WebDriverWait wait= new WebDriverWait(driver,15);

       wait.until(ExpectedConditions.visibilityOf(element));

  • Fluent wait: This is the best wait I feel. It provides you the flexibility to create your own condition to wait for.

Here's the code for Explicit Wait:

WebDriver driver= new ChromeDriver();

WebDriverWait wait= new WebDriverWait(driver, 30);

WebElement Element= wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("Some Expression"))));

Element.click();

Explanation:

  1. WebDriverWait is a class. We can create an object of it by passing WebDriver instance and timeout in seconds. WebDriverWait will ignore instances of NotFoundException that are encountered (thrown) by default in the ‘until’ condition, and immediately propagate all others. You can add more to the ignore list by calling ignoring(exceptions to add)

  2. An exception will be called based on expected condition in case of time out and condition is not satisfied. We can pass timeout only in seconds.

  3. ExpectedConditions is a class that has canned ExpectedConditions which are generally useful within web driver tests. For example: elementToBeClickable etc.

  4. All canned expected conditions are static methods of ExpectedConditions class. To view all canned conditions, do” ExpectectedCondition.(dot)”.

  5. This waits up to 30 seconds before throwing a TimeoutException or if it finds the element will return it in 0 – 30 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. Successful return value for the ExpectedCondition function type is a Boolean value of true, or a non-null object.

  6. Like implicit wait, the explicit wait will also not wait further if the condition is satisfied before timeout reaches.

  7. The explicit wait is intelligent to wait as it waits dynamically for the specified conditions.

I hope it helps!

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training!

+1 vote
by (33.1k points)

It seems like you want to find the element before locating the one with the issue solved it for me. Then simply call Tab on that element.

For example:

from selenium.webdriver.common.keys import Keys

elem = br.find_element_by_name("username")

elem.send_keys(Keys.TAB) # tab over to not-visible element

Now you can perform further functions on the element.

Hope this answer helps you!

Browse Categories

...