I did a google Search for the word 'Apple' using Selenium.
Using the below xpath i am trying to get hyperlinks only if the search results that has the word 'Cupertino' in it. But it does return nothing!
//a[contains(.,'Apple') and //span/em[contains(.,'Cupertino')]]
Google Search Results Screenshot:
Can someone help me out with correcting the Xpath i used?
Try below XPath
//h3[contains(.,'Apple') and following-sibling::div[contains(.,'Cupertino')]]/a
Related
I would like to know how to write the Xpath for validate the text -'Confirm Passowrd*' where there is more than 10 space gap between two words
When I tried to get it using chropath tool it gives Xpath like
//label[contains(text(),'Confirm Password*')]
But even that is also not working.
you can use the normalize-space in xpath.
//label[#ng-if='add_user' and normalize-space(text())='Confirm Password*']
You can also get text by:
driver.findElement(By.xpath("//label[#ng-if='add_user']")).GetAttribute("innerText");
OR
driver.findElement(By.xpath("//label[#ng-if='add_user']")).GetAttribute("value");
driver.findElement(By.xpath("//label").GetAttribute("value");
Scenario:
open "google.co.in".
click in the search input box.
type something.
click Enter.
get the text of all links.
The xpaths of some links are:
.//*[#id='rso']/div[2]/div/div[2]/div/div/h3/a
.//*[#id='rso']/div[2]/div/div[3]/div/div/h3/a
.//*[#id='rso']/div[2]/div/div[4]/div/div/h3/a
.//*[#id='rso']/div[2]/div/div[5]/div/div/h3/a
.//*[#id='rso']/div[2]/div/div[6]/div/div/h3/a
All the xpaths have the same pattern. the third div needs to be incremented by 1 to get the next xpath. I've read somewhere that in the scenarios like this generic xpath can be used. According to his suggestion, the xpath will be ".//*[#id='rso']/div[2]/div/div/div/div/h3/a". just removed the predicate of the third div. This is not working. Is this the way to locate elements?
You can try below XPath to fetch all result links:
//div[#class="g"]//h3/a
If you want to avoid links from "People also ask" block:
//div[#class="g"]//h3/a[not(ancestor::div[#class="_OKe"])]
I have a celltable , where i have multiple rows and is a dynamic table,
Screenshot attached ..
What i want is to find the row where column contains useren#lim.mobi
and then click its checkBox.
I am trying with xpath but not much experience here , If i can get some help please
Thanks
Here is the html code of the specific cell
I don't know the exact html of your table to form an xpath would be difficult.
however it should look something like this
//*[contains(text(),'useren#lim.mobi ')]/../td[2]
for following table, if I have to find the corrosponding contact for some company, This is how I would do it.
https://www.w3schools.com/html/html_tables.asp
//*[contains(text(),'Magazzini Alimentari Riuniti')]/../td[2]
Please try with the following xpath:
//tr[./td[contains(text(),'useren#lim.mobi')]/td[2]/input
if above xpath is not working please attach html code screenshot so i can tell you the extact xpath.
This might help to solve your issue:
//td[#title="useren#lim.mobi"]/following::input[#type="checkbox"]
If you get NoSuchElementException with this XPath you might need to add ExplicitWait as below:
WebElementWait wait = new WebElementWait(getWebDriver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//td[#title='useren#lim.mobi']/following::input[#type='checkbox']")));
I am using XPath to find exact value:
//h5[#class='familyName productFamilyName'][contains(text(),'Dozers ')]
but it was failing because in my application there are 2 elements with text values "Dozers " and "Dozers wheel" which is come under same class.
I can't use id locators,because it is dynamically generating in my application like //div[#id="482"]/div/div[1]/h5.
Please suggest me any solution.
If you want to match element with exact innerHTML value just use
//h5[#class='familyName productFamilyName'][text()='Dozers')]
or
//h5[#class='familyName productFamilyName'][text()='Dozers wheel')]
Depending on HTML structure you might need to use [.='Dozers'] or
[normalize-space(.)='Dozers'] instead of [text()='Dozers']
So in the webpage I am trying to write some selenium for, there is a label that causes more text to be displayed. I am trying to figure out how to have selenium select the label and then have the rest of the text displayed.
The button/label I am trying to get it to select is <label class="btn_expand" for="kpanel">Show Full Display</label>
When I tried using select I got an error "Element should have been select, but
instead was label". This was the code I tried -
Select select = new Select(driver.findElement(By.xpath("//label[#class=' btn_expand']")));
select.deselectAll();
select.selectByVisibleText("Show Full Display");
Thanks in advance.
I actually fixed this problem by using:-
driver.findElement(By.xpath("id(//label[text() = 'Show Full Display']/#for)")).click();
Read this to learn what a SELECT HTML tag is. You don't want a SELECT tag, you want the LABEL tag.
Your XPath is not efficient... it contains more than is actually needed to find that element. XPath, in general, is error prone, fragile, and slow. I prefer CSS Selectors. Try this
driver.findElement(By.cssSelector("label[for='kpanel']")).click();