clicking next button on reddit with selenium not doing anything - java

private void next() {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.reddit.com/r/pics/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement element = driver.findElement(By
.xpath("//span[contains(.,'next')]"));
element.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println(driver.getCurrentUrl());
is this code correct, all that happens when next is clicked is the focus scrolls down the page to the button

Your xpath selector is wrong.
Change it to:
WebElement element = driver.findElement(By
.xpath("//a[contains(text(),'next')]"));
or even better (in case one of the topic links contains the text "next") use:
WebElement element = driver.findElement(By
.xpath("//span[#class='nextprev']/a[contains(text(), 'next')]"));
This will ensure that the a element that is picked up is within the correct span at the bottom of the page and make your test less brittle.

The xpath = //span[contains(.,'next')] used in your code, locates span with contents view more : next › ,but you need to click only on next ›.i.e., you need to click on the anchor tag which contains next ›.
The below code will solve the issue.
WebDriver driver = new FirefoxDriver();
driver.get("http://www.reddit.com/r/pics/");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement element = driver.findElement(By
.linkText("next ›"));
element.click();
System.out.println(driver.getCurrentUrl());

It is always better to avoid xpath and use other locators like linkText or partialLinkText in this case.

Related

Click the Button and get the size of table in Selenium java

I am trying to learn selenium to find elements and I could not clicking the "Edit"buttons and Im trying the get the size of the rows of the table.
Here is my code:
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/";
driver.get(baseUrl);
driver.manage().window().maximize();
TimeUnit.SECONDS.sleep(5);
List<WebElement> we = driver.findElements(By.linkText("Edit")); System.out.println(we.size());
we.get(1).click();
Here is the link that I am working:
https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/
driver = new ChromeDriver();
driver.get("https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
WebElement frame = wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.id("demoFrame"))));
driver.switchTo().frame(frame);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[class=\"dx-link dx-link-edit\"]"))).click();
Use webdriver wait and switchTo frame, you have to switch to iframe first to interacte elements inside the iframe.
once you finish , if you want to interact with elements outside iframe then you have to switch out of it first.
driver.switchTo().defaultContent();
//rest of your code
The element is present inside an iframe and you need to switch iframe first and then you will be able to interact with element.
To overcome synchronization issue you need to induce WebDriverWait() and wait for frameToBeAvailableAndSwitchToIt() and following locator.
To get the link details induce WebDriverWait() and wait for visibilityOfAllElementsLocatedBy() and following locator.
WebDriver driver = new ChromeDriver();
String baseUrl = "https://js.devexpress.com/Demos/WidgetsGallery/Demo/DataGrid/PopupEditing/Angular/Light/";
driver.get(baseUrl);
driver.manage().window().maximize();
new WebDiverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("demoFrame")));
List<WebElement> we =new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.linkText("Edit")));
System.out.println(we.size());
we.get(1).click();

Selenium/Java No Such Element Exception for elements of a page after following a link from homepage

New to automation and could use some help here.
I am using Selenium Webdriver and Java on this website - Webdriver University and so far this code has been throwing No Such Element exception at "element.click()" step (i.e., doesn't find the element on page):
driver.manage().window().maximize();
driver.get("http://webdriveruniversity.com");
Thread.sleep(3000);
// Follow the link to another page
WebElement link = driver.findElementByXPath("(//div[#class=\"section-title\"])[6]");
link.click();
Thread.sleep(3000);
// Click on the element
WebElement element = driver.findElementByXPath("(//button[#class='accordion'])[1]");
element.click();
However, when I go to the linked page directly, it finds the element just fine
driver.manage().window().maximize();
driver.get("http://webdriveruniversity.com/Accordion/index.html");
// Click on the element
WebElement element = driver.findElementByXPath("(//button[#class='accordion'])[1]");
element.click();
I've used wait for element visibility and Thread sleeps, same results.
Any idea what could be the issue here?
Did you notice that when you click the link, page is opened in new tab? That is your issue.
You need to switch to new tab.
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1)); //here you are switch to second tab
Hope the below code will solve your issues.
Used the getWindowHandles() to capture handle of newly opened tab and switch to the
tab
// Follow the link to another page
WebElement link = driver.findElement(By.xpath("(//div[#class=\"section-title\"][6]"));
link.click();
Set<String> allWindow = driver.getWindowHandles();
Iterator<String> itr = allWindow.iterator();
while (itr.hasNext()) {
String wind = itr.next().toString();
driver.switchTo().window(wind);
}
Thread.sleep(3000);
// Click on the element
WebElement element = driver.findElement(By.xpath("(//button[#class='accordion'][1]"));
element.click();
Thread.sleep(3000);
driver.close();
}

Stale Element Exception, Element not Clickable,Element Click Intercepted & No Such Element error displayed Alternately by same element

I am trying to select multiple filers available in https://www.jabong.com/find/men's-black-jeans using Automation(Firefox).
However, after selecting the first option (Gender), I am unable to proceed.
I faced the Element click intercepted and tried Fluent Wait which lead to Stale Element Exception.
If I remove the Fluent wait or use implicit wait, I sometimes get the Element found exception.
To add to the confusion, sometimes the code runs properly without any wait and am able to select multiple options, but that is rare
public void case9() {
driver.get("https://www.jabong.com/");
WebElement SearchBox = driver.findElement(By.xpath("//*[#id=\"search\"]"));
SearchBox.sendKeys("men's black jeans");
Actions actn = new Actions (driver);
actn.sendKeys(SearchBox, Keys.ENTER).build().perform();
driver.manage().timeouts().implicitlyWait(34, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id=\"allFilterPopupTop\"]")).click();
driver.manage().timeouts().implicitlyWait(34, TimeUnit.SECONDS);
driver.findElement(By.xpath("/html/body/section[1]/div/section/section[1]/div/div[2]/div[2]/ul/li[1]/div[3]/div/div[2]/label[1]/div/input")).click();
driver.findElement(By.xpath("//*[#id=\"Brand\"]")).click();
WebDriverWait wait1 = new WebDriverWait(driver,61);
WebElement brand1 =
driver.findElement(By.xpath("xpath for element"));
wait1.until(ExpectedConditions.elementToBeClickable(brand1));
brand1.click();
driver.findElement(By.xpath("xpath for element")).click();
driver.findElement(By.xpath("//[#id=\"Global_Size\"]")).click();
WebElement Size =
driver.findElement(By.xpath("xpath for element"));
wait1.until(ExpectedConditions.elementToBeClickable(Size));
Size.click();
driver.findElement(By.xpath("//*[#id=\"Fit\"]")).click();
driver.manage().timeouts().implicitlyWait(61, TimeUnit.SECONDS);
driver.findElement(By.xpath("xpath for element")).click();
driver.findElement(By.xpath("//*[#id=\"Fade\"]")).click();
driver.manage().timeouts().implicitlyWait(61, TimeUnit.SECONDS);
[#id=\"applyFIlters\"]")).click();
}
I just tried and this code works fine.
driver.get("https://www.jabong.com/");
WebElement searchBox = driver.findElement(By.cssSelector("#search"));
searchBox.sendKeys("men's black jeans");
WebElement searchIcon = driver.findElement(By.cssSelector("#top-search-input > div.search-containter > span"));
searchIcon.click();
WebElement firstCheckBox = driver.findElement(By.id("boys"));
firstCheckBox.click();

How to interact with the elements within #shadow-root (open) while Clearing Browsing Data of Chrome Browser using cssSelector

I had been following the discussion How to automate shadow DOM elements using selenium? to work with #shadow-root (open) elements.
While in the process of locating the Clear data button within the Clear browsing data popup, which appears while accessing the url chrome://settings/clearBrowserData through Selenium I am unable to locate the following element:
#shadow-root (open)
<settings-privacy-page>
Snapshot:
Using Selenium following are my code trials and the associated errors encountered:
Attempt 1:
WebElement root5 = shadow_root4.findElement(By.tagName("settings-privacy-page"));
Error:
Exception in thread "main" org.openqa.selenium.JavascriptException: javascript error: b.getElementsByTagName is not a function
Attempt 2:
WebElement root5 = shadow_root4.findElement(By.cssSelector("settings-privacy-page"));
Error:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"settings-privacy-page"}
Attempt 3:
WebElement root5 = (WebElement)((JavascriptExecutor)shadow_root4).executeScript("return document.getElementsByTagName('settings-privacy-page')[0]");
Error:
Exception in thread "main" java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebElement cannot be cast to org.openqa.selenium.JavascriptExecutor
Incase if it is helpful the initial code block (till the above line) works perfect:
driver.get("chrome://settings/clearBrowserData");
WebElement root1 = driver.findElement(By.tagName("settings-ui"));
WebElement shadow_root1 = expand_shadow_element(root1);
WebElement root2 = shadow_root1.findElement(By.cssSelector("settings-main#main"));
WebElement shadow_root2 = expand_shadow_element(root2);
WebElement root3 = shadow_root2.findElement(By.cssSelector("settings-basic-page[role='main']"));
WebElement shadow_root3 = expand_shadow_element(root3);
WebElement root4 = shadow_root3.findElement(By.cssSelector("settings-section[page-title='Privacy and security']"));
WebElement shadow_root4 = expand_shadow_element(root4);
PS: expand_shadow_element() works flawless.
If you are trying to get 'Clear Data' element then you can use the below js to get the element and then perform.
return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-basic-page').shadowRoot.querySelector('settings-section > settings-privacy-page').shadowRoot.querySelector('settings-clear-browsing-data-dialog').shadowRoot.querySelector('#clearBrowsingDataDialog').querySelector('#clearBrowsingDataConfirm')
Here is the sample script.
driver.get("chrome://settings/clearBrowserData");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement clearData = (WebElement) js.executeScript("return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-basic-page').shadowRoot.querySelector('settings-section > settings-privacy-page').shadowRoot.querySelector('settings-clear-browsing-data-dialog').shadowRoot.querySelector('#clearBrowsingDataDialog').querySelector('#clearBrowsingDataConfirm')");
// now you can click on clear data button
clearData.click();
Edit 2: Explanation
Problem: Selenium does not provide explicit support to work with Shadow DOM elements, as they are not in the current dom. That's the reason why we will get NoSuchElementException exception when try to access the elements in the shadow dom.
Shadow DOM:
Note: We will be referring to the terms shown in the picture. So please go through the picture for better understanding.
Solution:
In order to work with shadow element first we have to find the shadow host to which the shadow dom is attached. Here is the simple method to get the shadow root based on the shadowHost.
private static WebElement getShadowRoot(WebDriver driver,WebElement shadowHost) {
JavascriptExecutor js = (JavascriptExecutor) driver;
return (WebElement) js.executeScript("return arguments[0].shadowRoot", shadowHost);
}
And then you can access the shadow tree element using the shadowRoot Element.
// get the shadowHost in the original dom using findElement
WebElement shadowHost = driver.findElement(By.cssSelector("shadowHost_CSS"));
// get the shadow root
WebElement shadowRoot = getShadowRoot(driver,shadowHost);
// access shadow tree element
WebElement shadowTreeElement = shadowRoot.findElement(By.cssSelector("shadow_tree_element_css"));
In order to simplify all the above steps created the below method.
public static WebElement getShadowElement(WebDriver driver,WebElement shadowHost, String cssOfShadowElement) {
WebElement shardowRoot = getShadowRoot(driver, shadowHost);
return shardowRoot.findElement(By.cssSelector(cssOfShadowElement));
}
Now you can get the shadowTree Element with single method call
WebElement shadowHost = driver.findElement(By.cssSelector("shadowHost_CSS_Goes_here));
WebElement shadowTreeElement = getShadowElement(driver,shadowHost,"shadow_tree_element_css");
And perform the operations as usual like .click(), .getText().
shadowTreeElement.click()
This Looks simple when you have only one level of shadow DOM. But here, in this case we have multiple levels of shadow doms. So we have to access the element by reaching each shadow host and root.
Below is the snippet using the methods that mentioned above (getShadowElement and getShadowRoot)
// Locate shadowHost on the current dom
WebElement shadowHostL1 = driver.findElement(By.cssSelector("settings-ui"));
// now locate the shadowElement by traversing all shadow levels
WebElement shadowElementL1 = getShadowElement(driver, shadowHostL1, "settings-main");
WebElement shadowElementL2 = getShadowElement(driver, shadowElementL1,"settings-basic-page");
WebElement shadowElementL3 = getShadowElement(driver, shadowElementL2,"settings-section > settings-privacy-page");
WebElement shadowElementL4 = getShadowElement(driver, shadowElementL3,"settings-clear-browsing-data-dialog");
WebElement shadowElementL5 = getShadowElement(driver, shadowElementL4,"#clearBrowsingDataDialog");
WebElement clearData = shadowElementL5.findElement(By.cssSelector("#clearBrowsingDataConfirm"));
System.out.println(clearData.getText());
clearData.click();
You can achieve all the above steps in single js call as at mentioned at the beginning of the answer (added below just to reduce the confusion).
WebElement clearData = (WebElement) js.executeScript("return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-basic-page').shadowRoot.querySelector('settings-section > settings-privacy-page').shadowRoot.querySelector('settings-clear-browsing-data-dialog').shadowRoot.querySelector('#clearBrowsingDataDialog').querySelector('#clearBrowsingDataConfirm')");
Screenshot:
I had to do a similar test which required clearing browsing the chrome history. A minor difference was that I was clearing the data after going to the advanced section of the pop-up. As you are struggling to click only the "Clear data" button, I'm quite sure that you've missed one or two hierarchy elements mistakenly. Or got confused between sibling and parent elements probably. As per seeing your code, I assume that you already know that to access a particular shadow DOM element you need proper sequencing and it has been explained also quite nicely above.
Coming right at your problem now, here is my code snippet which is working correctly. The code waits until the data is cleaned and then will proceed to your next action-
public WebElement expandRootElement(WebElement element) {
WebElement ele = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].shadowRoot",
element);
return ele;
}
public void clearBrowsingHistory() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 15);
driver.get("chrome://settings/clearBrowserData");
// Get shadow root elements
WebElement shadowRoot1 = expandRootElement(driver.findElement(By.xpath("/html/body/settings-ui")));
WebElement root2 = shadowRoot1.findElement(By.cssSelector("settings-main"));
WebElement shadowRoot2 = expandRootElement(root2);
WebElement root3 = shadowRoot2.findElement(By.cssSelector("settings-basic-page"));
WebElement shadowRoot3 = expandRootElement(root3);
WebElement root4 = shadowRoot3
.findElement(By.cssSelector("#advancedPage > settings-section > settings-privacy-page"));
WebElement shadowRoot4 = expandRootElement(root4);
WebElement root5 = shadowRoot4.findElement(By.cssSelector("settings-clear-browsing-data-dialog"));
WebElement shadowRoot5 = expandRootElement(root5);
WebElement root6 = shadowRoot5
.findElement(By.cssSelector("cr-dialog div[slot ='button-container'] #clearBrowsingDataConfirm"));
root6.click();
wait.until(ExpectedConditions.invisibilityOf(root6));
}
It should work properly in your case too if you don't intend to change any of the options selected by default in the pop-up (In that case, you will have to add a few more codes regarding selecting those checkboxes). Please tell me if this solves your issue. Hope this is helpful
I've added a snapshot of the the screen here too-
image
The Locator Strategy in #supputuri's answer using document.querySelector() works perfect through google-chrome-devtools
However, as the desired element opens from the shadow-dom you need to induce WebDriverWait for the elementToBeClickable() and you can you the following solution:
Code Block:
driver.get("chrome://settings/clearBrowserData");
new WebDriverWait(driver, 5).until(ExpectedConditions.elementToBeClickable((WebElement) ((JavascriptExecutor)driver).executeScript("return document.querySelector('settings-ui').shadowRoot.querySelector('settings-main').shadowRoot.querySelector('settings-basic-page').shadowRoot.querySelector('settings-section > settings-privacy-page').shadowRoot.querySelector('settings-clear-browsing-data-dialog').shadowRoot.querySelector('#clearBrowsingDataDialog').querySelector('#clearBrowsingDataConfirm')"))).click();
System.out.println("Clear data Button Clicked");
Console Output:
Clear data Button Clicked
I was getting InvalidArgumentEXception when trying to identify shadowRoot element in DOM using Selenium 4.3.0 and Chrome Version 103.0.5060.134
The solution to this is
SearchContext se= driver.findElment(By.locator("...").getShadowRoot(); return type is SearchContext
in the above line try using locator as xpath
and secondly trying to locate element using SearchContext reference e.g.
WebElement we= se.findElement(By.locator("....."));
use locater as cssSelector
And boom it works like charm
Didn't find this solution available and took me half a day to figure out
Hope this helps!!!

How to select an option from a dynamic dropdown using Selenium?

I am trying to click on dropdown value to select city in from field in Make my trip http://www.makemytrip.com/. But getting Stale element reference exception. Ids are getting changed on page load.
Tried below code:
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[#id='ui-id-1']"));
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeSelected(driver.findElement(By.xpath(".//*[#class='ui-menu-item'][2]"))));
To click on a dropdown value e.g. Mumbai you can use the following solution:
Code Block:
driver.get("https://www.makemytrip.com/")
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='input_fromto checkSpecialCharacters ui-autocomplete-input' and #id='hp-widget__sfrom']"))).click();
List<WebElement> myList = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//li[#class='ui-menu-item'][starts-with(#id,'ui-id-')]//span[#class='autoCompleteItem__label']")));
for (WebElement element:myList)
if(element.getText().contains("Mumbai"));
element.click();
Browser Snapshot:
You can use this working code:
WebDriver driver = new ChromeDriver();
driver.get("https://www.makemytrip.com/");
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).clear();
driver.findElement(By.xpath(".//*[#id='hp-widget__sfrom']")).click();
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#class='ui-menu-item'][2]/div/p[1]/span[1]"))).click();
I have fixed the xPath of dropdown list element. Always try to specify the exact element yo want to interact with. For example if you want to click on button, try to find <span> or <button> tag, for a link <a> tag and for input fields <input> tag.
You can try this code :
I do not see any use of xpath in this scenario. I have converted some of the xpath to either css selector or id. and have kept only one. Though I have not faced any stale element reference.
System.setProperty("webdriver.chrome.driver", "D:\\Automation\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("https://www.makemytrip.com/");
WebElement from = wait.until(ExpectedConditions.elementToBeClickable(By.id("hp-widget__sfrom")));
from.click();
from.clear();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul[class*='ui-widget-content hp-widget__sfrom']")));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[contains(#aria-label,'Top Cities : Mumbai, India ')]"))).click();
The below code works fine for me and it is parameterized as well, it works for any input value without changing the xpath. In this example, I took mumbai as test data.
driver.get("https://www.makemytrip.com/");
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[contains(#id,'hp-widget__sfrom')]")).clear();
driver.findElement(By.xpath("//input[contains(#id,'hp-widget__sfrom')]")).click();
driver.findElement(By.xpath("//input[contains(#id,'hp-widget__sfrom')]")).sendKeys("Mumbai");
Thread.sleep(2000);
WebDriverWait wait = new WebDriverWait(driver, 30);
By option = By.xpath("//div[#class='autoCompleteItem']/p/span[contains(text(),'Mumbai')]");
wait.until(ExpectedConditions.elementToBeClickable(option));
driver.findElement(option).click();

Categories

Resources