How to force Selenium WebDriver to click on element which is not currently visible?
Selenium determines an element is visible or not by the following criteria (use a DOM inspector to determine what css applies to your element, make sure you look at computed style):
- visibility != hidden
- display != none (is also checked against every parent element)
- opacity != 0 (this is not checked for clicking an element)
- height and width are both > 0
- for an input, the attribute type != hidden
Your element is matching one of those criteria. If you do not have the ability to change the styling of the element, here is how you can forcefully do it with javascript (going to assume WebDriver since you said Selenium2 API):
((JavascriptExecutor)driver).executeScript("arguments[0].checked = true;", inputElement);
But that won't fire a javascript event, if you depend on the change event for that input you'll have to fire it too (many ways to do that, easiest to use whatever javascript library is loaded on that page).
The source for the visibility check -
https://github.com/SeleniumHQ/selenium/blob/master/javascript/atoms/dom.js#L577
The WebDriver spec that defines this -
https://dvcs.w3.org/hg/webdriver/raw-file/tip/webdriver-spec.html#widl-WebElement-isDisplayed-boolean
Getting Element is not currently visible and so may not be interacted with error in Selenium webdriver for below HTML
Try using explicit wait and Expected Conditions
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.annotator-adder"))).click();
This will wait up to 20 seconds for the element to be visible before the click()
.
Unable to handle the element when it is not in the Visible Screen
See, since Selenium Webdriver
tries to simulate real users, it cannot interact with elements which are invisible/hidden. First you can move to the element using Actions
and then simply put a check for the visibility of elements, something like below:
WebElement element = driver.findElement(By by);
Actions action = new Action(driver);
action.moveToElement(element).perform();
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element)).WhateverActionYouWantToDoOnWebElement;
Then you can perform whatever action you want to perform.
Selenium webdriver not able to select element.Getting Element is not currently visible and so may not be interacted with error
Try to use explicit wait and expected conditions
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("compName")));
element.click();
This will wait up to 10 seconds for the dropdown to be visible.
element not interactable: Element is not currently visible and may not be manipulated
To select an item from the Housing type dropdown on the page you provided, I would first invoke WebDriverWait
on the dropdown menu to ensure that it exists before you try to interact with it. Then, you can use Javascript to click the dropdown trigger and expand the options.
After that, we invoke WebDriverWait
once more on the option we wish to click. The following code sample will click the option 'flat':
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# start driver
driver = webdriver.Chrome(executable_path=r'D:/apps/chromedriver/chromedriver.exe')
driver.get('https://post.craigslist.org/k/tKNKfCkr6hG7ghq71YXqTA/oj7w8?s=edit')
# wait for dropdown to exist
WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "//label[contains(@class, 'housing_type')]")))
# expand housing type dropdown using javascript
dropdown_trigger = driver.find_element_by_xpath("//label[contains(@class, 'housing_type')]/span/span[contains(@class, 'ui-icon')]")
driver.execute_script("arguments[0].click();", dropdown_trigger)
# select an option -- this selects 'flat'
dropdown_option = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.XPATH, "//li[text()='flat']")))
dropdown_option.click()
Related Topics
Quickest Way to Find Missing Number in an Array of Numbers
Booleans, Conditional Operators and Autoboxing
Getting the Class Name from a Static Method in Java
Create Java Console Inside a Gui Panel
How to Run Linux Commands in Java
Java Type Inference: Reference Is Ambiguous in Java 8, But Not Java 7
Should I Return a Collection or a Stream
Java 8's Streams: Why Parallel Stream Is Slower
Print an Integer in Binary Format in Java
Lambda Expression and Generic Defined Only in Method
Rationale for Matcher Throwing Illegalstateexception When No 'Matching' Method Is Called
Date Format Mapping to JSON Jackson
Getting Java Version at Runtime
Purpose of a Constructor in Java