This question already has answers here:
getWindowHandles() not working in firefox 58.The focus remains on parent tab and does not transfer to next tab
(2 answers)
Best way to keep track and iterate through tabs and windows using WindowHandles using Selenium
(1 answer)
Closed 3 years ago.
I am trying to automate a test case using Java, Selenium and Chrome Driver but I have not been able to navigate to the newly opened tab. I can see that the page opens but Selenium's focus does not move to the new tab.
I have tried a number of things: each of these points were tried on their own
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL, Keys.PAGE_DOWN);
element = driver.switch_to_window(element.driver.window_handles[1]);
element.sendKeys(Keys.CONTROL +"\t");
new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD2).build().perform();
element = driver.findElement(By.xpath("//bdi[contains(text(),'Open SQL Console')]"));
ScriptUtils.waitFor(DELAY);
supa.startMeasurement("40_Open_SQL_Console");
element.click();
System.out.println("Step 40 " );
ScriptUtils.waitFor(DELAY);
``` java
element = driver.switch_to_window(element.driver.window_handles[1]);
element.sendKeys(Keys.CONTROL +"\t");
new Actions(driver).sendKeys(driver.findElement(By.tagName("html")), Keys.CONTROL).sendKeys(driver.findElement(By.tagName("html")),Keys.NUMPAD2).build().perform();
I have even tried to go directly to the page but without luck.
driver.get ("https://mo-xxxx.mo.xxxx.corp:51043/xxxx/xxxxxa/cst/catalog/index.html?target=sqlconsole&databaseid=C119");
I can tell that I am on the same page because I can find an element on the current page.
org.openqa.selenium.NoSuchElementException: no such element: Unable to
locate element:
{"method":"xpath","selector":"//*[contains(#data-xxxx-ui,
'__editor')]"}
The element can easily be found outside of java/selenium so I know it is there.
Related
My flow is as follows in selenium:
Access a webpage
Click a tab
Click on add button in a tab where a window would open
Close that window
Click on that same tab again
I'm able to go through steps 1 to 4 without issues however at step 5 i'm not able to click the tab element knowing that i've clicked that same tab at step2, I did check from console and the same xpath I used in step2 did not return any element however when i clicked on that element to inspect it in console it started returning some values in console but still didn't work from selenium when i continued the run (in debug)
My page is in an Iframe which i was successfully able to access and print page title after step 4 but my issue remains, why aren't i able to click that tab from selenium after i close the window and the screen refreshes, why are the elements unresponsive anymore?
After doing some research i also tried the below script:
WebElement element=driver.findElement(By.xpath(the_path));
JavascriptExecutor ex=(JavascriptExecutor)driver;
ex.executeScript("arguments[0].click()", element);
but it would fail at the first line since the element is not fount, tried to initialize it earlier in the code but then it would fail at line 3.
How do i fix this issue?
Try to click on the element using Selenium code, not by injecting JavaScript to the pages. This should solve your problem.
WebElement element = driver.findElement(By.xpath(the_path));
element.click();
The issue was due to having multiple Iframes in the main page, so when closing switching windows I was not redirected to the proper Iframe in which i was in step 3 when clicking the tab.
This question already has answers here:
How to resolve ElementNotInteractableException: Element is not visible in Selenium webdriver?
(6 answers)
org.openqa.selenium.ElementNotInteractableException: Element is not reachable by keyboard: while sending text to FirstName field in Facebook
(5 answers)
Selenium WebDriver throws Exception in thread "main" org.openqa.selenium.ElementNotInteractableException
(1 answer)
Closed 2 years ago.
This is the DOM structure.
DOM
Any of the following operations are throwing org.openqa.selenium.ElementNotInteractableException
selectFileStatus.selectByIndex(2);
Or
selectFileStatus.selectByVisibleText(valueToSelect);
The instantiation is like this.
Select selectFileStatus = new Select(wait.until(ExpectedConditions.elementToBeClickable(By.xpath(
property.getPropertyObject(propFileDetailClaimReview).getProperty(propertyDropdownFileStatus)))));
And the XPATH is tested in browser.
//span[contains(text(), 'File Status')]/parent::label/following-sibling::select
Interestingly, a read operation is working fine.
List<WebElement> allOptions = selectFileStatus.getOptions();
System.out.println("*** Values of the dropdown...");
for(WebElement eachOption : allOptions)
System.out.println(eachOption.getText());
As this <select> tag looks like a real HTML object, what can be the problem here?
Note: Tried JS injection, and it did nothing actually (I expected it to expand the dropdown).
WebElement ele = driver.findElement(By.xpath(
property.getPropertyObject(propFileDetailClaimReview).getProperty(propertyDropdownFileStatus)));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
This question already has answers here:
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
(9 answers)
Closed 4 years ago.
I'm trying to locate the element by XPath: I tried to add wait to load the page and also tried to scroll the page down. but same error
driver.findElement(By.xpath("//text()[contains(.,'Add portfolio')]/ancestor::a[1]")).click();
unknown error: Element ... is not clickable at point (326, 302). Other element would receive the click: ...
Add ExplicitWait to wait for the element to be visible/clickable
webDriverWait waitForElement = WebDriverWait(new TimeSpan(0, 0, 15));
waitForElement.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(by));
If there is any overlay that appears, ensure its closed since it might obstruct the element from getting the click
This might help you but make sure there is no alert or popup active in the window.
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_of_elm")));
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.xpath("xpath_of_elm"))).click().build().perform();
I would suggest you to use JavascriptExecutor click method here.
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
If this is still not working, then check if there is any loading image there on the screen; you would be required to wait for that image to disappear in that case.
This question already has answers here:
Wait for page load in Selenium
(48 answers)
Closed 6 years ago.
I am trying to automate some test cases using Java and Selenium WebDriver. I have the following scenario:
There is a page named 'Products'. When I click on 'View Details' link
in the 'Product' page, a popup (modal-dialog) containing the details of the item appears.
When I click on the 'Close' button in the popup the popup closes and
the page automatically refreshes (the page is just reloading, the contents remain unchanged).
After closing the popup I need to click on 'Add Item' button in the
same page. But when WebDriver trying to find the 'Add Item' button,
if the internet speed is too fast, WebDriver can find and click the
element.
But if the internet is slow, WebDriver finds the button before the
page refresh, but as soon as the WebDriver click on the button, the page refreshes and StaleElementReferenceException occurs.
Even if different waits are used, all the wait conditions become true
(since the contents in the page are same before and after reload)
even before the page is reloaded and StaleElementReferenceException
occurs.
The test case works fine if Thread.sleep(3000); is used before clicking on the 'Add Item' button. Is there any other workaround for this problem?
3 answers, which you can combine:
Set implicit wait immediately after creating the web driver instance:
_ = driver.Manage().Timeouts().ImplicitWait;
This will try to wait until the page is fully loaded on every page navigation or page reload.
After page navigation, call JavaScript return document.readyState until "complete" is returned. The web driver instance can serve as JavaScript executor. Sample code:
C#
new WebDriverWait(driver, MyDefaultTimeout).Until(
d => ((IJavaScriptExecutor) d).ExecuteScript("return document.readyState").Equals("complete"));
Java
new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
Check if the URL matches the pattern you expect.
It seems that you need to wait for the page to be reloaded before clicking on the "Add" button.
In this case you could wait for the "Add Item" element to become stale before clicking on the reloaded element:
WebDriverWait wait = new WebDriverWait(driver, 20);
By addItem = By.xpath("//input[.='Add Item']");
// get the "Add Item" element
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));
//trigger the reaload of the page
driver.findElement(By.id("...")).click();
// wait the element "Add Item" to become stale
wait.until(ExpectedConditions.stalenessOf(element));
// click on "Add Item" once the page is reloaded
wait.until(ExpectedConditions.presenceOfElementLocated(addItem)).click();
You can do this in many ways before clicking on add items:
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.id("urelementid"))); // instead of id you can use cssSelector or xpath of your element.
or:
wait.until(ExpectedConditions.visibilityOfElementLocated("urelement"));
You can also wait like this. If you want to wait until invisible of previous page element:
wait.until(ExpectedConditions.invisibilityOfElementLocated("urelement"));
Here is the link where you can find all the Selenium WebDriver APIs that can be used for wait and its documentation.
yes stale element error is thrown when (taking your scenario) you have defined locator strategy to click on 'Add Item' first and then when you close the pop up the page gets refreshed hence the reference defined for 'Add Item' is lost in the memory so to overcome this you have to redefine the locator strategy for 'Add Item' again
understand it with a dummy code
// clicking on view details
driver.findElement(By.id("")).click();
// closing the pop up
driver.findElement(By.id("")).click();
// and when you try to click on Add Item
driver.findElement(By.id("")).click();
// you get stale element exception as reference to add item is lost
// so to overcome this you have to re identify the locator strategy for add item
// Please note : this is one of the way to overcome stale element exception
// Step 1 please add a universal wait in your script like below
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // just after you have initiated browser
There are two different ways to use delay in selenium one which is most commonly in use. Please try this:
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
second one which you can use that is simply try catch method by using that method
you can get your desire result.if you want example code feel free to contact me defiantly I will provide related code
I'm trying to use Selenium (Java) to automate some searches.
When I go to the page and click Inspect Element on the search bar, I see that it has the id searchBox and name q, both of which are potentially useful. Nevertheless, these properties are nowhere to be found in the HTML when I go to View Source.
When I try to use
WebElement search = driver.findElement(By.id("searchBox"));
or
WebElement search = driver.findElement(By.name("q"));
both come back as unable to be found.
How do I proceed with populating the search field then hitting submit (the submit button also is missing in the source page) if I can't find either of these elements?
EDIT:
As requested, here's a more detailed picture of what's wrong:
The URL of the page accessed by WebDriver is http://www.ycharts.com using the line
driver.get("http://www.ycharts.com/login");
If you go to this page with your actual web browser, right click on the search bar and choose Inspect Element, you'll see that its ID is "searchBox" and its name is "q". However, if you go to view the same page's source, you'll see that there's no such element in the HTML. Clearly, this is why WebDriver can't find it.
driver was initiated as follows:
WebDriver driver = new HtmlUnitDriver();
When I try something like
WebElement search = driver.findElement(By.id("searchBox"));`
The exception returned is:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element with ID: searchBox
So, back to the original question: clearly, the element is there, but it's not in the HTML – how do you interact with it?
The problem is being caused by the fact that the search box is added to the html post page load by javascript.
The HTML returned by http://ycharts.com/ does not contain the searchbox, therefore when Selenium thinks the page has finished loading (i.e DOM ready state), there is no search box.
In order to interact with the search box, you need to tell Selenium to wait until the box appears in the DOM.
The easiest way to achieve this is with WebDriverWait;
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id<locator>));