How can I get text from element with Selenium webdriver and Java? - 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"));

Related

cant find the element using classname in selenium java

I'am using selenium framework to test my own website. Im trying to click on specific icon which using anchor tag. I have java selenium code to click but couldn't click. Tried many xpath, css selectors, class name and names. but didn't worked. But can run the script and it is opening the chrome and navigating to entered domain but the clicking option is not working
above code I need to click nav-twitter class anchor option . which will create another tab in chrome to show my twitter page. but after running the app .it is only navigating to the page domain and nothing works.
So, This is my code where I have added. until the maximize everything works but not the anchor tag
This kind of error im getting when running the script in chrome. Please anyone let me know where I have been wrong here or is there are any way to make it happen. Thanks in advance.
Whenever you are initiating webdriver make sure to add implicit wait statement, so it can wait for sometime before looking for objects. Add below statement right after chromedriver initialization and your code should work without any issues.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Your locator is slightly off. In your code you are looking for an element with tagName as twitter which doesn't exists. Instead you can use either of the following locator strategies:
Using cssSelector:
WebElement navTwitter = driver.findElement(By.cssSelector("a.nav-twitter[name='twitter']"));
navTwitter.click();
Using xpath:
WebElement navTwitter = driver.findElement(By.xpath("//a[#class='nav-twitter' and name='twitter']"));
navTwitter.click();
You have used wrong selector/locator here. "twitter" is not tag name, Tag name is the value from less than symbol to the first space or greater than symbol. Words like div, p, a, span are the tag names.
So here a is the tag name and name, class, href are attribute which you can use while forming xpath or css locators. So you can create multiple locators here:
By using class name attribute
By.className("nav-twitter");
By creating xpath using name attribute
By.xpath("//a[#name='twitter']");]
By creating xpath using class attribute
By.xpath("//a[#class='nav-twitter']");
By creating css locator using class attribute.
By.cssSelector("a[class*='nav-twitter']");
By using dot symbol in css selector with class name
By.cssSelector("a.nav-twitter");
Similarly you can create css selector with name too. Since there are multiple tags, so you can not use a tag directly and If you want to use By.tagName("a") then it will click on first tag present on the page
Note: We use dot with class name and Hash (#) with Id name while forming css selectors.

how to click in href link with selenium-java

i'm working with selenium-java to automate some tests (it's self learning). I'm stuck in click a hyperlink, but this href is pretty particular, because like this:
<a tabindex="-1" href="../../myWebPage.html"><span>My Web Page</span></a>
My java code is:
1.- driver.findElement(By.xpath("//a[#href='../../myWebPage.html']")).click();
2.- driver.findElement(By.xpath("//a[#href='https://RealHost/pag1/myWebPage.html']")).click();
The second option that i used is with the real link, but non of them is working.
Could you please help me?
P.S: I also used the option driver.findElement(By.LinkText("https://RealHost/pag1/myWebPage.html")).click(); but without success.
Thanks guys!
You should be able to locate the link element by first finding its parent element by looking through the DOM and getting the xpath for that.
Then use that parent element to find elements with a tag of "a"
WebElement parent = findElement(By.xpath("/*path to parent element here*/"));
parent.findElement(By.tagName("a")).click();
Note that the parent element may have multiple children of tagName "a" if that is the case use findElements() to get a collection of all the hyperlinks with that parent. Then search the collection for the one you want.
WebElement parent = findElements(By.xpath("/*path to parent element here*/"));
List<WebElement> elements = parent.findElements(By.tagName("a")).click();
//search the list for the correct link
Another thing you could try would be to locate the element by linkText.
findElement(By.linkText("/*The hyperlinks text*/")).click();
hope this helps!
Try either of the xpath.
driver.findElement(By.xpath("//a[contains(#href,'/myWebPage.html')]//span[text()='My Web Page']")).click();
OR
driver.findElement(By.xpath("//a[.//span[text()='My Web Page']]")).click();
OR css selector
driver.findElement(By.cssSelector("a[href*='/myWebPage.html']>span")).click();
this works for me:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("My Web Page"))).click();
i added a wait overe the element to be clickable and it's works
Thanks guys for your help.
Bye
this works fine for IE driver, but with Chrome is failing due to:
stale element reference: element is not attached to the web page
Help please!

How do I get an xpath using Firepath plugin

I'm not able to get an xpath using FirePath. I want to locate username field of a login form using xpath in Firepath. But when I try to find the same element,
I'm getting xpath like :
html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input
How can I use it in Selenium script ?
You can use it like:-
WebElement ele1 = driver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input"));
ele1.sendKeys("username");
Here username in double quotes would be replaced by actual value you want to enter in username field.
This is also xpath which is called absolute xpath If you are looking for relative xpath then uncheck the option as shown in below image
And you can use this xpath like :
driver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input")).sendKeys("username");
You can do this via the by.xpath
webdriver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input")).click();
You can also search for a relative xpath witch is more robust have a look at this for more information of xpath.
Also have a look at locators in selenium for more information about the diffrent locators

XPATH issue. Not able to read parts of html

I need to able to click on a link (I am using Selenium and Java). I am searching the link using xpath but for some reason I am not getting most of the webpage, just a bunch of white spaces. In the image you can see the highlighted link I am looking for.
I tried:
System.out.println(driver.findElement(By.xpath("//*[#class='titre_1']/a")).getText());
System.out.println(driver.findElement(By.xpath("//*[#id='li-7']/div/a")).getText());
I get: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element
If I do:
System.out.println(driver.findElement(By.xpath("//*")).getText());
I only get a few elements from the page and bunch of white spaces. What could be wrong?
Please help. I couldn't fit in the entire html source to show you. I hope that's ok.
html source pic
In case the element is inside an iframe, you can get it by switching to iframe then call findElement. See code below:
WebElement iframeElement = driver.findElement(By.id("id_of_the_iframe"));
driver.switchTo().frame(iframeElement);
Then you can find the element with your xpath:
System.out.println(driver.findElement(By.xpath("//*[#class='titre_1']/a")).getText());

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