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

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

Related

How to find xpath for first searched product

How to find XPath for the first searched element on given page
https://www.amazon.com/b/?encoding=UTF8&node=2346727011&bbn=7141123011&ref_=Oct_d_odnav_1040660&pd_rd_w=WJf7p&pf_rd_p=72459b27-e231-4837-b61c-b057ff0c50ac&pf_rd_r=YMKJ7M0AMXW5GER3MMRQ&pd_rd_r=eba15a0a-f59c-4dfd-a693-7c2f7f3416cf&pd_rd_wg=LRrkE
I tried the below XPath..on html page its showing the element but when putting on selenium code it's showing the error invalid selector: Unable to locate an element with the xpath expression
//div[#id='CardInstance1wPgwM8pHmoJmdnyjYOeWg']//child::div[2]//div[1]//div[1]//div[1]//div[2]//div[#class='a-section a-spacing-none a-spacing-top-small s-title-instructions-style']//span
Please help me to find xpath for first searched product.
Thanks in Advance.
You can directly use
//li[starts-with(#class,'octopus-pc-item')]
this does have 26 entries and is not unique in HTML, however, findElement will return the first matching node.
Furthermore, you can do indexing like below
(//li[starts-with(#class,'octopus-pc-item')])[1]
and [2] for the second product and so on... for other products
Perform click like below
Using ExplicitWaits
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//li[starts-with(#class,'octopus-pc-item')]"))).click();
Using findElement
Thread.sleep(3000);
driver.findElement(By.xpath("(//li[starts-with(#class,'octopus-pc-item')])[1]")).click();

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 to get unique xpath for all the hyperlink dynamically in webpage

I have some web page which contains hyperlinks for some word, which also can be repeated. I need to verify the hyperlink for each word, e.g. the word headache which is 3 times in that page. After clicking on each headache the same page opens.
Is there any method which can return the XPath for all words headache because for the automation i need to click on every headache and verify the URL.
I could write the XPath manually but what if there are a lot of headache words on same page which have the hyperlink?
In xpath you have to declare type of element for example:
driver.findElements(By.xpath(".//link[text()='headache']")).forEach(WebElement::click)
Also if you need just to check the link is correct you can use Rest Assured library and send get requests by http.
You can try this xpath = //a[text()='headache'] or find with linkText driver.findElements(By.linkText("headache"))
List<Webelement> ele = driver.findElements(By.linkText("headache"))
for(Webelement link: ele){
link.click();
// other operation
}

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