Trouble getting an element to be clicked using Selenium - selenium

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.

Related

Element not interacable

I have this error "element not interactable" When I try to click a drop down list with selenium. However in debug mode, when I inspect ( press F12 ) before the break points and I continue to run then the test is passed. So my question is Why the elements can be clicked and what should I do to prevent the situation again. Many thanks!
You have to add wait / delay before accessing the elements to let the elements be fully loaded on the page before accessing them.
The simplest way is to add hardcoded sleep, like
time.sleep(5)
The better way is to use explicit way implemented by expected conditions.
Few things to note down,
Always launch browser in full screen mode.
driver.maximize_window()
this code should be written before driver.get()
Whenever you get element not interactable, try to use action chains to click or send_keys :
something like this :-
from selenium.webdriver.common.action_chains import ActionChains
action = ActionChains(driver)
action.move_to_element('your web element here').click().perform()
Make sure that using Selenium, the desired web element should be in Selenium view port.
in order to resolve this, you may have to scroll down to let Selenium know where exactly the element is.

How to click a row that has javascript on click event in RIDE

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

Selenium for VBA - element not found error

I have been using a code since long time , but recently there is a new banner up which is hiding the element that I am trying to click.
Attaching the snapshot of error. The only help I need is I need to click the hidden element( if the browser window is maximized the element is visible).
.
Please help me.
If what you have said is true then you can use the following to maximize the window before clicking:
driver.Window.Maximize
Other options include:
1) Removing the banner
2) Scrolling the element into view
Can't write anything decent for those last two as your code is an image and I don't have a full URL to test with. You also haven't included the relevant HTML.
The "div.container-fluid" element is blocking the button you are trying to click.
You could try some of the following (as being shown here Element MyElement is not clickable at point (x, y)... Other element would receive the click):
prolong the wait before the click
use javascript executor

Selenium WebDriver Explicit Wait intermittently not working

Good day everyone,
I need your help in this method. I have a web page that will have a loading screen UI when the page loads, and I'm waiting for it to finish before clicking a button.
Here is my code:
#Step("Go to Audit Inquiry Screen")
public void launchAuditInquiry(){
WebDriver webDriver = Driver.webDriver;
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loading-container")));
WebElement auditInquiryBtn = webDriver.findElement(By.linkText("Audit Inquiry"));
auditInquiryBtn.click();
}
My issue is sometimes, this code works fine. It will wait for the loading ui div to be invisible before clicking the button. But sometimes it will produce this error:
Error Message: org.openqa.selenium.WebDriverException: unknown error: Element <a class="module-item" href="/audit/inquiry">...</a> is not clickable at point (822, 436). Other element would receive the click: <div class="loading-container" style="display: flex; opacity: 0.899842;">...</div>
I tried adding another explicit wait before clicking the button to be sure, like this:
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.className("loading-container")));
WebElement auditInquiryBtn = webDriver.findElement(By.linkText("Audit Inquiry"));
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Audit Inquiry")));
auditInquiryBtn.click();
But it will sometime produce the same error above, and sometimes it will work fine.
I'm just confused on how to remediate the issue.
Thank you guys for the comments, especially this: Selenium Web Driver & Java. Element is not clickable at point (36, 72). Other element would receive the click
It was helpful, but some items there, I have already tried, but did not work as well. The part that I did check was one item there and also a comment here also which is to force a click:
new Actions(driver).moveToElement(auditInquiryBtn).click().perform();
But I'm having second thoughts on this because, a scenario may happen that when the loading container div is still overlaying the page, then I forced clicked the submit button, it will also produce another loading container div, and I'm not sure what will happen if there are two loading container div present.
Now, my solution on this is to adjust the sleep timer of the wait function:
WebDriverWait wait = new WebDriverWait(webDriver, 10, 2500L);
It now works because it gives the loader div time to generate before the first check of wait. 500 ms was a bit fast for the loader to render. I'm still testing this but if it didn't work, I might do the solution above.
Thanks again.
First thing to try is remove the invisibilityOfElementLocated wait and just use elementToBeClickable. I've never really trusted what Selenium considered "visible" and "invisible".
I've had issues in the past where the element to click on was completely off screen, so Selenium automatically scrolled until it was considered in the viewport. But because of a floating footer, it didn't scroll enough and was still behind the footer so could not be clicked on. It was still considered "visible" because it was in the viewport.
But, if you're sure, you can try forcing a click at a coordinate instead of an element.
new Actions(driver).moveToElement(auditInquiryBtn).click().perform();

How to send a click to hidden button?

I have a simple submit button on a page that I need to send a click for it but this button is at the bottom of the page and does not appear unless a user uses the browser vertical scroll bar to reach it !!
so when I use this C# code I get error element is not visible so how I can solve this problem ?
driver.FindElement(By.Id("submit")).Click();
You could scroll down using javascript. Something like this should do the work.
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
Selenium Webdriver implicitly scroll to the element, if it is visible. You can wait for the visibility of the element and then perform the click operation. In Java, we can wait for the visibility using following code:
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(driver.findElement(By.id("submit"))));
In the above code, Webdriver will wait for 60 seconds, for the required element to become visible. If within 60 seconds, the element does not appear, then it will fail with timeout error. Once, the required element is visible, it can be clicked.
If it's not visible by you, it is not render by the browser used through your selenium driver.
I recommend you to simulate a scrolling to ensure your item will be visible, or do a simulation on a bigger resolution ?
Eventually you can consider switch to an headless driver...
I found the problem there are two elements in the page with id="submit" !! I used Name and now it work fine, thank to all.