XPATH issue. Not able to read parts of html - java

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());

Related

Unable to click on a "text" link in google search page

I am trying to click on News link on google search page the HTML structure looks like this
I tried following xpaths but none worked
//a/child::span[1][contains(.,'News')]
The following xpath resulted in invalid selector: The result of the xpath expression "//a/child::span/following-sibling::text()[contains(.,'News')]" is: [object Text]. It should be an element.
//a/child::span/following-sibling::text()[contains(.,'News')]
Thanks
//a[contains(.,'News')] might return this link, but may result in a list of more than one element that you'd need to handle and select the right element from.
You can use Selenium's SearchContext to specify a container element, or solve it using an xpath one-liner like: //div[#role='navigation']//a[contains(.,'News')] (Effectively searching for a link that contains 'News' somewhere in it's html-tree, somewhere inside a div that has a role attribute with value 'navigation').
You simply need
//a[contains(., "News")]
Note that "News" is not a part of span, but a, so your 1st XPath won't work

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!

Getting the text from the element in iframe with Selenium, Java

I am trying to get the text in the element, which is within an iframe. How can i get the right Xpath or CSS- Selector? I have switched to the frame, but the compiler is unable to locate the element with xpath or cssSelector. Probably the problem is that I am contentiously falling to write the right path and selector. All experiments with right click- copy the path or writing the path by myself have no result.
Here is my code to switch to the iframe and get the text.
driver.switchTo().frame("iframe-analytics");
Thread.sleep(10000);
String st=driver.findElement(By.xpath("//div[#id='container']/div/div[2]/span")).getText();
System.out.println(st);
here is the html for the element and the element itself
html
the element
finally this works for me
String st=obj.findElement(By.xpath("//*[#id=\"container\"]/div/div[2]/span")).getText();
System.out.println(st);

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"));

Java Selenium. Find element by text for certain xpath

Trying to find an xpath expression to use in:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("XPATH HERE"))).click();
The element says "Invite Users" on the page and i need to be able to click on it
I need to find the element /a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle'] but the problem is the characters after "inviteUsers_" is dynamically generated
I have already tried these:
"//*[contains(.,'Invite Users')]";
"//a[contains(.,'Invite Users')]";
And these give NoSuchElement exceptions.
This is the complete XPATH:
/html/body/div[#class='col-xs-10 col-xs-offset-2 main']/fieldset[#class='form-horizontal']/div[#id='roles']/div[#id='entitlements']/div[#class='panel panel-default '][3]/div[#id='service_8ef17ba4-b739-4fb6-8198-3862ea84c381']/div[#class='panel-body']/div[#class='panel panel-default'][1]/div[#class='panel-heading']/h4[#class='panel-title']/a[#id='inviteUsers_8ef17ba4-b739-4fb6-8198-3862ea84c381_toggle']
You can solve it with starts-with():
//a[starts-with(#id, "inviteUsers_")]
If this does not work try find a unique parent.
//div[contains(#id, 'service')]//a[contains(#id, 'inviteUsers')]

Categories

Resources