How to find element using the value attribute with Selenium and XPath - selenium

In selenium I used the following XPATH to find all elements which have the word ok:
//button[(((#value='ok')) or ((#value='Ok')) or ((#value='OK')))]
What if I want to find all buttons which start with the word ok? As an example OK1 is a match.

To find all elements which have the word ok you can modify as follows:
//button[#value='ok' or #value='Ok' or #value='OK']
and to find all elements which starts with the word ok you can use:
//button[starts-with(#value, 'ok') or starts-with(#value, 'Ok') or starts-with(#value, 'OK')]

Related

How to find the previous element of an element found in Appium Xpath?

PROBLEM:
I am having an issue to identify an element in Appium.
As I can't identify the element, I managed to identify by xpath the next element. Let's call it "FOUND" element.
SO now I try to get the previous element from this element "FOUND".
DETAILS:
In this screenshot above, you can see the elements I am speaking about.
To find the "FOUND" element, I am looping in all the element with the class "android.widget.TextView", I extract the attribute 'text' and compate it to the string 'Website'.
Then from the element FOUND, I try to find the element I need. I tried so many various expression, but I didn't succeed to get it. I use a "try, except" to try to cath it, but without success.
here is the code:
elements_of_profile_detail_page = driver.find_elements_by_class_name("android.widget.TextView")
list_xpath=[
"preceding-sibling::android.widget.TextView[1]",
"preceding-sibling::android.widget.TextView[1]",
"(/preceding-sibling::android.widget.TextView)[1]",
"/*preceding-sibling::android.widget.TextView[1]",
"(/*preceding-sibling::android.widget.TextView)[1]",
"(preceding-sibling::android.widget.TextView)[1]",
"../android.widget.TextView[0]",
"preceding-sibling::*[1]",
"/preceding-sibling::android.widget.TextView",
"preceding-sibling::android.widget.TextView",
"(preceding-sibling::android.widget.TextView)[1]"
]
i=0
while i<len(list_xpath):
try:
website = element_of_profile_detail_page.find_element_by_xpath(list_xpath[i]).get_attribute('text')
print(f"website : {website}")
print(f"xpath : {xpath}")
break
except:
print("It didn't work!")
i+=1
And here is another screenshot with more details of the element I need:
I am using Appium 1.15.1 and Python 3.7. I don't think it is important as it is a matter of Xpath.
I hope I gave enough details to find the solution. I am working on it since very early this morning.
You can directly find the element by using its text in the xpath like:
element = driver.find_element_by_xpath("//*[contains(#text,'AlfangeAcademy')]")

Selenium xpath failing to find element (other xpath tools prove it's there)

Selenium FindElement:
driver.FindElement(By.XPath($"//*[contains(text(), '{text}')]"));
Throws:
no such element: Unable to locate element:
{
"method":"xpath",
"selector":"//*[contains(text(), '269424ae-4d74-4a68-91e0-1603f2d674a0')]"
}
(Session info: chrome=74.0.3729.169)
(Driver info:
chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),
platform=Linux 4.18.0-20-generic x86_64)
But it's definitely there and the xpath is valid because I can use AngleSharp to parse the driver's page source with the same xpath expression:
new HtmlParser()
.ParseDocument(driver.PageSource)
.SelectSingleNode($"//*[contains(text(), '{text}')]");
The target element is a div containing a guid:
<div class="home-project-title-text"> 269424ae-4d74-4a68-91e0-1603f2d674a0 </div>
This is with
dotnet core 2.2
chrome webdriver
Chrome 74
Ubuntu 18.04
EDIT1
Interestingly the document.evaluate in the browser console also fails with this xpath expression. I use this as a helper function for running xpath:
selectSingle = xpath => document.evaluate(xpath, document).iterateNext()
and then find that this returns null:
> selectSingle("//*[contains(text(), '269424ae-4d74-4a68-91e0-1603f2d674a0')]")
> null
but it's definitely there and has the expected text, e.g. I can use a different xpath expression to manually locate and check it's text content:
> selectSingle("//*[#id='app']/div/div[1]/div[3]/div/div[1]/div/div[1]/div")
.textContent
.trim()
== "269424ae-4d74-4a68-91e0-1603f2d674a0"
> true
EDIT2
So the cause was that the div was being created in react like this:
React.createElement(
"div",
{className = "home-project-title-text"},
" ",
"269424ae-4d74-4a68-91e0-1603f2d674a0",
" ");
I think this roughly means that the div has three textnodes as children (is that valid?). The result looks 100% normal - it renders perfectly and inspecting the element with devtools looks like a single text node and .textContent returns the concatenated string.
Now that you gave some more information (how this element is created):
Yes, it is possible that an XML element has as its children several separate text nodes. However, this is usually not the case if the text nodes are adjacent to each other, instead of separated by child elements.
If '269424ae-4d74-4a68-91e0-1603f2d674a0' is indeed the second text node, then
//*[contains(text(), '269424ae-4d74-4a68-91e0-1603f2d674a0')]
will indeed not return this div element. You should not think of this as "breaking XPath", it is just that the precise semantics of the expression are:
Find an element with any name whose first text node contains '269424ae-4d74-4a68-91e0-1603f2d674a0'.
text() actually selects all text nodes of an element, but XPath functions such as contains() silenty select only the first one.
What you actually would like to select is
an element with any name where any text node contains '269424ae-4d74-4a68-91e0-1603f2d674a0'
And an expression to achieve exactly that is:
//*[text()[contains(.,'269424ae-4d74-4a68-91e0-1603f2d674a0')]]
You can test those expressions with a small toy document such as:
<div className="home-project-title-text">
<other/>
269424ae-4d74-4a68-91e0-1603f2d674a0
<other/>
</div>
Where other elements are forcing the div element to contain three separate text nodes, two of them containing whitespace only.
Finally, if you already know that the element you are looking for is a div, then you should look specifically for that:
//div[text()[contains(.,'269424ae-4d74-4a68-91e0-1603f2d674a0')]]
It might be the case the element lives in an iframe, if this is the case - you will have to use IWebDriver.SwitchTo() function in order to switch to the required iframe prior to attempting locating the element.
It might be the case the element is not immediately available, i.e. it's being loaded by an AJAX request, in that case you will need to use WebDriverWait class so Selenium could wait till the element appears in DOM prior to interacting with it.
Try the following xpath.See if you get any luck.
//div[#class='home-project-title-text'][contains(.,'269424ae-4d74-4a68-91e0-1603f2d674a0')]
EDIT
//div[contains(.,'269424ae-4d74-4a68-91e0-1603f2d674a0')]

Selenium - Select element based on multiple attributes via Xpath

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

How to locate links from Google results

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

How to find exact value using xpath in selenium webdriver?

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']