Selenium: Element could not be scrolled into view - java

I am trying to learn Selenium using java with Eclipse. I am trying to locate one element in the webpage.
I tried using this line of code to click on one html <a> element:
driver.findElement(By.xpath("//a[#href='itemDetail.php?id=19']")).click();
The picture below shows the html structure of the page:
With that piece of code, I ran into an exception:
Element <a href="itemDetail.php?id=19"> could not be scrolled into
view
Did I use the wrong syntax to locate the element?
Thank you for the advice.

The locator looks good as you haven't received NoSuchElement exception. Try the below javascript click, which does not need the element to be scrolled into view.
JavaScript Click
WebElement element = driver.findElement(By.xpath("//a[#href='itemDetail.php?id=19']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();",element);
If you want to scroll the element into view and the do normal click user the below code.
Scrolling into view and normal click:
WebElement element = driver.findElement(By.xpath("//a[#href='itemDetail.php?id=19']"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();",element);
element.click();

To locate and click() on the desired <a> element you can use the following solution:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='container']//figcaption[text()='Play-Doh Rainbow Starter Pack']//preceding::img[1]"))).click();

Related

xPath Text Contains Selenium Web Driver

I'm trying to select an element based on its text contents. I am using XPath to achieve this.
I am just puzzled as this should work?
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I'll even copy the HTML code:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
The div element has the words "Notifications" inside it. So why doesn't it work.
Go to this page on Facebook: https://www.facebook.com/settings
Use this chrome extension to highlight any area via xPath.
You have a space before the word Notifications:
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
You should also add a wait for element before trying to find the element:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(), 'Notifications')]"));
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I found the issue with the help of some amazing people in this community.
Ok, so my element was in an iFrame.
In order to access the element, I must first access the iFrame
WebElement iframe = obj.driver.findElement(By.xpath("//iframe[#tabindex='-1']"));
obj.driver.switchTo().frame(iframe);

Selenium can't find iframe after postback inside the iframe after finding one element inside the frame

I am trying to create a test where I have to fill out some information inside an iframe. Getting access to the iframe work fine and I can fill out information inside the frame. The issue is that when I find out a element 'A' it has a postback attached to it which reloads the content inside the iframe, to find out another element 'B'. So i am not able to find that element.I am getting below error:
org.openqa.selenium.WebDriverException: unknown error: Element <iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7" cd_frame_id_="7da8f2aea5a580b3a6e90a9d5016fa0d">...</iframe> is not clickable at point (554, 7). Other element would receive the click: <div class="topnav2014" style="border-bottom: none;">...</div>
(Session info: chrome=85.0.4183.83)
Here are my observations: When I first locate the iframe it looks like this:
<iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7">
After the postback has occurred it looks like this:
<iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7" cd_frame_id_="a5006acf28d8c288313681ab9ad7a4fa">
I can easily find element A:
But element B i am not able to find
The code fails when I try to get hold of the iframe element.
How can I get hold of the iframe again, after the postback inside the frame?
I have tried this suggestion also but it is not working
//Ensure that you are back to the base frame
driver.SwitchTo().DefaultContent();
//SwitchTo the intended frame
driver.SwitchTo().Frame(driver.FindElement(By.XPath("//iframe[contains(#src,'<removed for clearity>')]")));
Use a driver.executescript() for the first problem since another element is receiving the click.
element = driver.findElement(By.id(""));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
This error message...
org.openqa.selenium.WebDriverException: unknown error: Element <iframe style="overflow-x:hidden;" id="t5" height="1350" frameborder="0" width="98%" src="https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1&days=7" cd_frame_id_="7da8f2aea5a580b3a6e90a9d5016fa0d">...</iframe> is not clickable at point (554, 7). Other element would receive the click: <div class="topnav2014" style="border-bottom: none;">...</div>
(Session info: chrome=85.0.4183.83)
...implies that the WebDriverException was raised as you tried to invoke click() on the <iframe> element.
Factually, instead of clicking on the <iframe> element you would invoke click() on an element within the <iframe>. Moreover, as the the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe_xpath")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();
References
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
I am able to extract the data of the active cases rise and fall for every state. There is a single frame which contains both the locators shared by you. Check below working code.
driver.get("https://www.ndtv.com/coronavirus");
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.className("tab-wrapper")));
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(#src,'https://edata.ndtv.com/coronavirus/table/india_table.html?shgraph=1')]")));
//cases down
List<WebElement> eleCasesUp = driver.findElements(By.xpath("//tr//td[3]//p//span[#class='data-up']"));
List<String> casesUpList = new ArrayList<String>();
for (WebElement element : eleCasesUp) {
casesUpList.add(element.getText());
}
//cases up
List<WebElement> eleCasesDown = driver.findElements(By.xpath("//tr//td[3]//p//span[#class='data-down']"));
List<String> casesDownList = new ArrayList<String>();
for (WebElement element : eleCasesDown) {
casesDownList.add(element.getText());
}
System.out.println("Cases Up List -->" + casesUpList);
System.out.println("Cases Down List -->" + casesDownList);

Unable to locate an element through Selenium and WebDriver

i am trying to click the save button. but i am not able to click it because before the button it has "::before".
i tried locating using cssSelector and xpath. but i am not able to locate it. can any one help me?
The element you are trying to locate is within a Modal Dialog Box so you have to induce WebDriverWait for the element to be clickable and you can use either of the following options :
cssSelector :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.modal-footer button.btn.btn-default#save"))).click();
xpath :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='modal-footer']//button[#class='btn btn-default' and #id='save']"))).click();

click on a button with JQuery

There is somthing wrong with the page I want to test.
My first try:
When I clicked manually on a button, then I will be forwarded normally on the next page.
When I tried to click on the same button with selenium, then I get an error page "Sorry...something gone wrong...blabla". I think this problem can only solve the developer team of the page.
By book = By.cssSelector("#button\\.buchung\\.continue");
//By book = By.cssSelector("button.buchung.continue");
//By book = By.xpath("//*[#id='button.buchung.continue']");
WebElement element= ConfigClass.driver.findElement(book);
element.click();
But I want to try a workaround:
I clicked on the same button with JQuery.
I opened my chrome console and execute the button with:
jQuery('#button\\.buchung\\.continue').click()
How can I execute this JQuery expression in my selenium code?
I tried this, but without success:
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("jQuery('#button\\.buchung\\.continue').click()");
Use $
je.executeScript("$('#button\\.buchung\\.continue').click()");
jQuery("selector") will return you a list.
I think you have to call click() on the element at index 0 (Assuming exactly one element satisfies the selector)
Code:
je.executeScript("jQuery('#button\\.buchung\\.continue')[0].click()");
You were pretty close. If the cssSelector is uniquely identifying the WebElement you can use the following code block :
By book = By.cssSelector("#button\\.buchung\\.continue");
WebElement element= ConfigClass.driver.findElement(book);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

How to scroll down a specific div of page having pagination using selenium webdriver

I am trying to scroll down page and get the last element of that section/div.
i have executed the code:
Coordinates coordinate = ((Locatable)element).getCoordinates();
coordinate.inViewPort();
and also tried with
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 250)");
But it is scrolling the entire page instead of scrolling the specific section
And also the page has pagination
First you need to find the element you're trying to scroll to:
WebElement element = driver.findElement(By.xpath("xpath_here")); //or anything else used to identify the element
Afterwards, you can execute JS using JavascriptExecutor to bring the element into view using scrollIntoView() :
((JavaScriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
What you have tried will scroll the window scroll bar, try something like below
JavascriptExecutor je = (JavascriptExecutor) dr;
je.executeScript("arguments[0].scrollIntoView(true);",dr.findElement(By...));
Specify the locator of the element which you are trying to find by scrolling down.
arguments[0]
Edit:
JavascriptExecutor je = (JavascriptExecutor) dr;
je.executeScript("arguments[0].scrollTop(arguments[0].scrollHeight);",dr.findElement(By...));

Categories