I used 3 different waits purposely to locate the element (a checkbox) on the page as shown below and they get passed. After that I click on that same element which also gets passed.
Now my question is if the click element method gets passed then why does Checkbox should be selected fails because in click element method I am clicking on that checkbox only!!
HTML screenshot.
I have tried this clicking on checkbox multiple times using various strategies but it fails every time. Please help and suggest some solution!!
Code I wrote:
` sleep 2
wait until page contains element id_service_levels_0
wait until element is enabled id=id_service_levels_0
wait until element is enabled id=id_service_levels_0
page should contain element id=id_service_levels_0
click element id=id_service_levels_0
checkbox should be selected id=id_service_levels_0
`
You can try with JavaScript executor as given below.
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('id_service_levels_0').click()");
Related
I am trying to go to the below URL
https://twitter.com/explore
Enter HBO Max in the text box which I locate using
By.cssSelector("input[placeholder='Search Twitter']")
and then I want to select the HBO Max option that appears in the dropdown. My locator for the element to be selected is
By.xpath("//div[#role='option']//li/div/div[2]")
Sometimes the element gets clicked and I go to the new page, sometimes not. I have in my framework waited for the element to be clickable using WebDriverWait. The element is both visible and enabled because I print these values before I click the element.
Additional debugging steps performed -
Click using JavaScript seems to have the same behavior.
I believe I am using the right locator because the mouse event gets generated for this element.
Thread.Sleep seems to work suggesting that perhaps a timing issue
Any inputs would be great.
try:
myElem = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'ID')))
print("Page is ready!")
except TimeoutException:
print("Loading took too much time!")
Try this !
what it does : It wait until the specific element of the site is loaded. If it takes more time than specified will just give you an output as "loading took too.........."
You can modify the time required to wait.
Selenium: How selenium identifies elements visible or not? Is is possible that it is loaded in DOM but not rendered on UI?
I would like to verify a scenario where element is clickable, I know web drive has method "ElementToBeClickable" however, I would like to know the inner working. Please help me on this.
Also,how to handle a scenario where the element is loaded in the DOM but UI shows loading in progress, how to wait for complete load?
Please let me know, if the UI is not loaded then will selenium directly call the DOM element or if UI element is being loaded then it will fail the execution? I would really appreciate more technical explanation on this.
Selenium can identify the presence or visibility of the elements as soon as they are present or visible in the HTML DOM. From user perspective you can invoke isDisplayed() method on an WebElement to examine if the intended WebElement is displayed or not. As per current implementation Selenium may not be distinguishing between loaded and rendered elements. The ElementToBeClickable method in ExpectedConditions class sets an expectation for checking if an element is visible and enabled so that you can click it.
When the element is loaded in the DOM but UI shows loading in progress you still have to wait for the JavaScript and AJAX Calls to complete loading the page so all the WebElements on the page becomes interactable. At most to wait for complete load you can set the pageLoadStrategy to normal but may still have to induce WebDriverWait for the intended WebElement to become present, visible, interactable or clickable.
Here you can find a detailed discussion on Page load strategy
Of-coarse if the UI is not loaded Selenium may not be able to interact with a few of the DOM elements.
Update
As per your counter question here are the different stages of an WebElement and the respective ExpectedConditions to check the stages :
Presence of an element :
presenceOfElementLocated(By locator)
An expectation for checking that an element is present on the DOM of a page. This does not necessarily mean that the element is visible.
Visibility of an element :
visibilityOf(WebElement element)
An expectation for checking that an element, known to be present on the DOM of a page, is visible. Visibility means that the element is not only displayed but also has a height and width that is greater than 0.
Element to be Clickable :
elementToBeClickable(By locator)
An expectation for checking an element is visible and enabled such that you can click it.
Note : As per the docs Element is Clickable - it is Displayed and Enabled.
I am trying to click a row that has a javascript onclick function in RIDE IDE by using Robot Frame Work with Selenium2 library.
<tr class="lstrow" onclick="javascript:selectItem(this);" onmouseover="javascript:this.className='rowhover';" onmouseout="javascript:this.className='row';">
When i perform click event at following xpath, it says element at path not found.
//*[#id="myList"]/tbody/tr[0]
By inspecting the element, I can confirm this row is there. Also tried to pin point the 'rowhover' class at this xpath but still not successful. Not even sure if I really can select a specific class at a specific xpath.
//*[#id="myList"]/tbody/tr[0][contains(#class,'rowhover')] //Not sure if it is right
The desired element is a JavaScript enabled element so you need to induce wait and you can use either/both (clubbing up) of the following solutions:
Python Solution
Wait Until Element Is Visible:
Wait Until Element Is Visible xpath=//tr[#class="lstrow"] 20 seconds
Set Focus To Element xpath=//tr[#class="lstrow"]
# invoke click
Wait Until Element Is Enabled:
Wait Until Element Is Enabled xpath=//tr[#class="lstrow"] 20 seconds
Set Focus To Element xpath=//tr[#class="lstrow"]
# invoke click
You can find a detailed discussion about Wait Until Element Is Visible and Wait Until Element Is Enabled in Robotframework: Selenium2Lib: Wait Until (…) Keywords
Reference: Selenium2Library
I am working in a travel application. Once i filled the details like origin, destination i have to click the submit button. I tried with the xpath to click the submit button. But, i am unable to submit. Application is loading internally,once it clicks the xpath of submit. Please refer screen shot.
Selenium code: java driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']")).click();
Please check image
Submitbutton- xpath code
can you please provide your html code for this issue,
there must be element present in that code,
you should have to use submit() instead of clicking on submit element.
actually click() works only on submit button of (you have to check this condition) and submit() works for all form elements and performs Enter key pressed like operations.
i hope this have to work for you,
driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']")).submit();
or try using this,
driver.findElement(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_tpTravel_travel_gvTravel_ctl02_txtDuration']")).submit();
according to your html code we get element by id,
driver.findElement(By.Id("ctl00_cphMain_TravelRequest1_btnSubmit")).submit();
In your case, might be the submit button is visible but not clickable as you mentioned in the description. For that, you will have to wait until the element is not to be clickable using the ExplicitWait method. Perform click action after the Submit button gets clickable:
Hope below code will solve your problem.
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//[#id='ctl00_cphMain_TravelRequest1_btnSubmit']"))).click();
Sometimes the click() method does not work. Please try with below code once and let me know.
WebElement Login=driver.findElement(By.id("ctl00_cphMain_TravelRequest1_btnSubmit"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", Login);
I am clicking on the text box displayed on first page
WebElement txtBox = driver.findElement(By.xpath("---xpath---"));
txtBox.click();
Then after some block of execution I am getting the same textbox in new page on same window.
Here also I want to click on the text box.
I used JavascriptExecutor to do this scripting.
((JavascriptExecutor)driver).executeScript("arguments[0].click();", txtBox );
But while running the script I am getting an error message saying:
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
You need to execute driver.findElement() every time you reload the page.
This method returns WebElement which is integral part of current page. If you refresh browser or navigate to some other URL or even if element is deleted and attached again by some javascript on same page, previously found element cannot be used anymore.
Here you have official explanation of this exception: http://www.seleniumhq.org/exceptions/stale_element_reference.jsp
Stale Element exception comes after you want to interact with an element loaded previously. If you get webelement and then reload the page it gives this exception because it is not in a newly created page. It is better to click the web element without assigning it to a variable like:
driver.findElement(By.xpath("---xpath---")).click();