Selenium WebDriver Java cssSelector Span - java

I am trying to click on a link using Selenium WebDriver in Java. My Java:
driver.findElement(By.cssSelector("span[data-seleniumid=\"Address0\"]")).click();
The HTML on my page looks like this:
<span data-seleniumid="Address0" class="ATAddressLine">1 The Road, Town, City, Postcode</span>
The error in Eclipse is:
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span[data-seleniumid=\"Address0\"]"}
Thanks

Instead of trying to escape the inner double quotes, just use a single quote instead.
driver.findElement(By.cssSelector("span[data-seleniumid='Address0']")).click();

I would try a different selector like "span.ATAddressLine". Not sure if webdriver likes your attribute "data-seleniumid".

Have a webdriver wait condition like waiting for an element to be clickable and then use your above code.

Thanks for you help all. The element wasn't being found because it was in an iframe popup and Selenium was searching for it in the page behind.
This post: https://stackoverflow.com/a/32836709/6565982 helped.
For anyone in the future my code is now:
WebElement iFrame= driver.findElement(By.tagName("iframe"));
driver.switchTo().frame(iFrame);
// Select an address
driver.findElement(By.cssSelector("span[data-seleniumid=\"Address0\"]")).click();
// Switch back to the default page
driver.switchTo().defaultContent();
Thanks again.

Related

How to click element under <a href> link using Selenium java

HTML code
using Selenium + Java. I need to click the filter button.
Currently, I am using below code but it doen't work.
WebElement filter = driver.findElement(By.linkText("Filter"));
filter.click();
Xpath - //*#id="frmBookingListing"]/div[3]/div1/div[3]/div/div1/div/div[2]/a[2]
cssSelector - .searchFilter
I don't know what's wrong with my code. please help to click on filter button.
Thanks in advance.
Try this:
.//a[#class='btn btn-typ4 searchFilter' and #title='Filter']/span

Scroll aside div with Selenium

I'm working on a program that uses Selenium and I'm trying to scroll on some aside div of the document.
I firstly tried the following line of code but this only scrolls the main part of the HTML document:
((JavascriptExecutor) driver).executeScript("scrollBy(0, 500)");
So I tried to execute this:
((JavascriptExecutor) driver)
.executeScript("document.getElementsByClassName('aside-div')[0].scroll(0,100)");
This compile and executes without any error but the section I want to scroll through doesn't scrolls. How can I do this?
The best practice is to always use the javascript code on your browser console before actual implementation.
Also, plz make sure scroll(x,y), the value should be according to the scroll it can be either (1000,0) or (0,1000) negative-positive according to the requirement. Kindly choose accordingly.
Ok so coming back to your actual problem, what I feel is you are incorrectly using the code
Either you have been using scroll(0,1000) instead of scroll(1000,0) so on..
You have not mentioned the exact element locator.
PS: Please provide the HTML for in-depth debugging.
Meanwhile, you can checkout the below code:
((JavascriptExecutor) driver).executeScript("document.getElementsByClassName('aside-div'[0].scroll(0,1000)");
//OR
((JavascriptExecutor) driver).executeScript("document.getElementsByClassName('aside-div'[0].scroll(1000,0)");
//OR
WebElement elementToScroll = driver.findElement(By.cssSelector(".aside-div"));
((JavascriptExecutor) driver).executeScript("arguments[0].scroll(1000,0);", elementToScroll);
Please provide the exact element locator to use below code:
Example:
WebElement elementToScroll = driver.findElement(By.cssSelector(".aside-div table tr td"));
//This will scroll the page Horizontally till the element is found
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", elementToScroll);

Unable to locate element - Java/Selenium

I'm trying to select the highlighted element below (which is a "window close" button):
There is one other element with class='icon-Dismiss' on the page, but none with class='dialog-close'.
What I have tried so far:
driver.findElement(By.xpath("//*[#class='icon-Dismiss' and #class='dialog-close']"))
driver.findElement(By.className("dialog-close"))
driver.findElement(By.xpath("//*[#id='contentBox']"))
In all cases however, I receive the following error:
no such element: Unable to locate element
Does anyone have an idea on how I can select this element?
You need to induce WebDriverWait for the desired elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.contentBox div.icon-Dismiss.dialog-close"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='contentBox']//div[#class='icon-Dismiss dialog-close']"))).click();
Try using
driver.findElement(By.cssSelector(".icon-Dismiss.dialog-close"))
Also, make sure this element is not in an iFrame. If it is then you will need to switch to the iFrame first before you can find the element.
try this driver.switchTo().alert().dismiss(); if that dialog box is an alert.
If you just want to locate then
driver.findElement(By.cssSelector("div.icon-Dismiss.dialog-close"));
but if you want to just close the alert then alert().dismiss(); will be the best option.
If this is the window pop up
try : driver.switchTo().alert().dismiss();
if this is application these locators should work
Note : make sure you are using waiting till popup appearing on page

How can I get text from element with Selenium webdriver and Java?

My code:
WebDriver driver = new SafariDriver();
driver.get("http://bet.hkjc.com/football/default.aspx");
WebElement matchs = driver.findElement(By.cssSelector("span.Head to Head"));
System.out.println(matchs);
driver.quit();
How can I crawl Manchester Utd and Celta Vigo?
WebElement matchs = driver.findElement(By.xpath("//a[#title='Head to Head']"));
System.out.println(matchs.getText());
Use firebug and firepath addons in firefox and inspect that element and get the xpath and put it here inside double quotes in this code :
System.out.println(driver.findElement(By.xpath("")).getText());
If you don't know how to use firebug and firepath refer this link
You can locate the element either by css selector or xpath selector
By using xpath
driver.findElement(By.xpath("//a[#title='Head to Head']"));
By using css Selector
driver.findElement(By.cssSelector("span > a[title='Head to Head']"));
OR Try somethings like this if not getting the match
driver.findElement(By.cssSelector("td.cteams.ttgR2>span>a[title='Head to Head']"));
Note : in your code you are trying like span.Head to Head in CSS selector . dot represents the class and according to your path you are locating span tag which have class name "Head to Head" which doesn't exist in your dom as this is the title of anchor tag.
Went through the Firebug and Firepath plugins of Firefox initially to get the Xpath or css path
Explore some blogs to get clear understanding, you will be able to create by yourself
Refer This link for the same
I assume all the above answers doesn't work for you and am providing another answer.
I can see both the texts are under "a" tag. So the idea is to navigate to the element and use getText() - which returns the visible text.
String word = driver.findElement(By.xpath("//span/a")).getText();
System.out.println(word);
Hope this works for you.
In all of my tests I'm using the getAttribute like this to get text and it is working fine for me on all drivers :
assertEquals(strCity, txtCity.getAttribute("value"));

webdriver classname with space using java

This question received great answers in jquery and I was wondering if someone could give an example of this in Java please?
I'm doing driver.findElement(By.className("current time")).click(); The space is the issue, and I see the explanation at the link, but I'm not sure how to handle it in java, and don't have access to change the class name.
Pasting example of what i get in the firefox inspect id: Example with cssSelector below did not work, but i may be missing something.
<span>
<a class="current time" href="http://someurl/" onclick="s_objectID="http://someur/">url</a>
</span>
Instead of class name you can use a css selector. You don't mention the tagname for the class 'current time'. I am assuming it to be input, so your css selector work be,
WebElement element = driver.findElement(By.cssSelector("input[class='current time']"));
element.click();
Edit#1 Based on html provided,
Looking at the html in your comment, it seems you have quite a few options to find the webElement. Here are your options,
WebElement element = driver.findElement(By.cssSelector("a[class='current time']"));
element.click();
or this should work too,
WebElement element = driver.findElement(By.cssSelector("a.current.time"));
element.click();
You can also use linkText since the element is link. From the html you provided, the link text is 'url'
WebElement element = driver.findElement(By.linkText("url"));
element.click();
You can also use By.partialLinkText("partial link text here");
You can also use xpath as:
WebElement element = driver.findElement(By.xpath("//a[#class='current time']"));
element.click();
OR,
WebElement element = driver.findElement(By.xpath("//a[text() = 'url']"));
element.click();
For a less fragile test, another option is to use an XPATH which doesn't depend of the order of classes, like:
WebElement element = driver.findElement(By.xpath("//a[contains(#class, 'current') and contains(#class, 'time')]"));
Whenever you found some space in the class name you need to switch to cssSelector Locator.
Convert a class name to cssSelector if it is having a space as below.
In your case it would be:
WebElement element = driver.findElement(By.cssSelector(".current.time"));
element.click();
PS: add . [dot] in start of class name and replace the space with . [dot] to convert class name to cssSelector.

Categories

Resources