Check if item is available in selenium

I can check if the item exists and is not displayed, but cannot find a way to see if it is "interactive" (not to mention disconnected).

The problem is that often when filling out a web form the element that I want can be superimposed on the div when loading. The div itself is quite difficult to detect, since it is id, name and even css is flexible. Therefore, I am trying to determine if the input field can "click" or "fill." When an overlay div is present, the field cannot be populated by the ordinary user (since the div will overlay the input field and will not allow the user to fill it), but it can be filled with selenium. I want to prevent this and only allow selenium to fill it as soon as the user can fill it.

+6
source share
3 answers

You can wait for the item to be clickable :

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = webdriver.Firefox() driver.get("http://somedomain") element = WebDriverWait(driver, 10).until( EC.element_to_be_clickable((By.ID, "myDynamicElement")) ) 

Or you can follow the EAFP principle and catch the exception thrown by click() :

 from selenium.common.exceptions import WebDriverException try: element.click() except WebDriverException: print "Element is not clickable" 
+9
source

You can use the webElement.isEnabled() and webElement.isDisplayed() methods before performing any operation in the input field ...

I hope this solves your problem ...

In addition, you can also put a loop to check the above 2 conditions, and if they are true, you can specify the input text, and again you can find the same element, and you can get the text of the web element and compare this text with text. which you entered. If these matches you can exit the loop.

+1
source

to click an element (for selenium), unavailable for use:

 public void clickElement(WebElement el) throws InterruptedException { ((JavascriptExecutor) driver).executeScript("arguments[0].click();", el); } 
0
source

Source: https://habr.com/ru/post/978232/


All Articles