Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Element not visible exception

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() 
like image 368
user4450305 Avatar asked Jan 13 '15 17:01

user4450305


People also ask

When an element is not visible exception is thrown?

If we try to locate a web element which is not visible or hidden on screen then we will get the element not visible exception. Synchronization problem: If Selenium is faster than application or vice versa. If we have used duplicate xpath in our script means more than one web element have same xpath.

How do you handle an element not found exception?

NoSuchElementException Ideally, the exception occurs due to the use of incorrect element locators in the findElement(By, by) method. To handle this exception, use the wait command. Use Try/Catch to ensure that the program flow is interrupted if the wait command doesn't help.

How does Selenium handle element not displayed?

First Solution: Try to write unique XPATH that matches with a single element only. Second Solution: Use Explicit wait feature of Selenium and wait till the element is not visible. Once it is visible then you can perform your operations.

How do you handle invisible hidden elements in Selenium?

Selenium by default cannot handle hidden elements and throws ElementNotVisibleException while working with them. Javascript Executor is used to handle hidden elements on the page. Selenium runs the Javascript commands with the executeScript method. The commands to be run are passed as arguments to the method.


1 Answers

If you look at the page source, you'll understand that almost all of theSELECT, DIV elements are faked and created from JavaScript, that is why webdriver cannot SEE them.

There's a workaround though, by using ActionChains to open your developer console, and inject an artificial CLICK on the desired element, which in fact, is the Label triggering the NBA data loading... here's a working example:

from selenium import webdriver from selenium.webdriver.common import action_chains, keys import time  driver = webdriver.Firefox() driver.get('Your URL here...') assert 'NBA' in driver.page_source action = action_chains.ActionChains(driver)  # open up the developer console, mine on MAC, yours may be diff key combo action.send_keys(keys.Keys.COMMAND+keys.Keys.ALT+'i') action.perform() time.sleep(3) # this below ENTER is to rid of the above "i" action.send_keys(keys.Keys.ENTER) # inject the JavaScript... action.send_keys("document.querySelectorAll('label.boxed')[1].click()"+keys.Keys.ENTER) action.perform() 

Alternatively to replace all the ActionChains commands, you can simply run execute_script like this:

driver.execute_script("document.querySelectorAll('label.boxed')[1].click()") 

There you go, at least on my local file anyway... Hope this helps!

enter image description here

like image 98
Anzel Avatar answered Sep 21 '22 07:09

Anzel