Unable to Identify All Text Elements on Page with XPath - java

I'm trying to identify all text elements on web page. Based on several threads I've read on stackoverflow I've come up with the following XPath:
//*[normalize-space(.)=.][not(self::script or self::style or self::meta)]//.
//*[normalize-space(.)=.][not(self::script or self::style or self::meta)]//text()
However, I noticed that while this identifies several hundred text elements on Amazon product page, it leaves out some important ones.
For example:
On this page I am not able to identify the 'features' section and order by time section:
"Echo Show brings you everything you love about Alexa.."
'Want it tomorrow.."
On this page I cannot identify the description section (immediately to the right of product image)
Both of these pages have text that are prominently displayed on the page so I do not see why it is not identifying them accordingly.
I'm aware that some of these text are under ul/li tags but I don't think this is the issue.
Finally, would it be possible to combine the said XPath with another attribute value so it only identifies text elements with given attribute value (i.e, //*[normalize-space(.)=.][not(self::script or self::style or self::meta)]//. + .//*[#id='XYZ'])
Thanks

Try using:
//*[text()[not(normalize-space()='')]]
That will select any element that has a text() node as a direct child that isn’t just whitespace.

Related

Selenium returning text from path not specified in xpath statement

I have text located in these two locations:
The elements I want the text from:
//*[#id="RoleSetUpNewForm"]/div/table/tbody/tr[1]/td[2]/table[2]/tbody/tr/td/div[1]/table[3]/tbody/tr[2]/td/table[1]/tbody/tr/td[2]/table[1]/tbody/tr/td/label
The elements that I do not want the text from:
//*[#id="home"]/div[16]/form/div/table/tbody/tr[1]/td[1]/table/tbody/tr[18]/td[3]/div/a
This xpath statement when executed in Selenium keeps giving me both the elements I want, AND the elements I do not want:
driver.findElements(By.xpath("//td[#class='level3TD']/label"));
Clearly the xpath above does not reference the location that I do not want the text from, but I am getting the text elements from it returned..
I am just baffled. Any ideas? Is there something I need to do with my selenium xpath query to stop the text from the path ending in /div/a from coming back?
There is no need to manually write XPath's.
Open the dev console by pressing F12 (assuming you are on a Windows machine).
Now find the node that you want, right click on it and click "Copy". Now you can copy the relative XPath, Full XPath, JS Path or Selector. In your case you look like you want the Full XPath.
This can be done on all machines, in all browsers.
I figured this out, there were duplicate label text on the page that made me think the xpath was pulling from the wrong location. Lets close this.

Why gettext() in selenium is returning a-singlespace-b instead of a-multiplespaces-b?

I am testing TodoMVC page's todo list and I came across a problem where in the DOM label tag of the element is having a text with many spaces between two letters, but on the UI it is showing letter single space then another letter, and when gettext() is performed on the element we are getting "a b" which is visible on the UI instead of the text present in label tag of that element.
This is how Selenium WebDriver works. Since it designed for UI testing, all interactions with Browser data are made from a user perspective.
Here is an extraction from Get Element Text W3C specification:
NOTE
The Get Element Text command intends to return an element’s text “as rendered”. An element’s rendered text is also used for locating a elements by their link text and partial link text.
One of the major inputs to this specification was the open source Selenium project. This was in wide-spread use before this specification written, and so had set user expectations of how the Get Element Text command should work. As such, the approach presented here is known to be flawed, but provides the best compatibility with existing users.

java Selenium Chrome - keyword searcher

I'm almost done creating a Supreme Bot. Now I need a keyword-searcher. They should search for a keyword on the page and then click on it.
For example:
Illegal Business Hooded Sweatshirt Red
... the bot now searches for the keyword but also for the color. I uploaded a screenshot (from the Supreme page) and need your help.
Screenshot from the source code (Supreme):
My code I tried:
driver.findElement(By.xpath("//h1[text()='Illegal Business Hooded Sweatshirt']/p[text()='Red']")).click();
Since there're encoded symbols in between of text, I believe you can't find those items directly with xpath.
I suggest you to find all <acticle> tags, then for every article tag you search for <h1> inside, retrieve it's text (with filtering out those weird symbols), and compare the text you want with the text article tag actually has.
p tage is not inside h1 tag and "red" is inside anchor tag a
So you can use this xpath:- //h1/a/[text()='Illegal Business Hooded Sweatshirt']/ancestor::div/p/a[text()='Red']

Accessing an element with unknown id but known path

I am working on a testing program that operates with very little information. In this particular case, my program doesn't know the ID of elements in the page before it runs, because the Javascript on the page dynamically assigns those at run time. The only constants I have is the structure and the text I'm looking for. I'm including a screenshot of one example of the DOM being generated. In this case I know that I want to access the button with text apply that is displayed next to the label with the text "To Location:" Is there a way to use xpath manipulate their relationship and ensure that I'm accessing the right element. I can't just access the apply button because there are 6 apply buttons on the page with dynamically generated IDs. The label's next to them are different so I'm trying to use that and manipulate the path's from there. Help?
This is possible. If you provide the entire html code I could provide a better xpath. But for what you pasted, here's a simple one that might work:
//td[div//label[text()='To Location:']]/following-sibling::td[1]//button[text()='Apply']
There's a slightly longer winded way but thats generating a list of elements by class and clicking the one with the right text
`var elements = driver.FindElements(By.Class("text-pb"));
foreach(var element in elements)
{
if(element.Text.Equals("Searched Text"))
{
element.click();
}
}`
that might work thats if you want to click the button.
i use these sort of things on the pages works site generates so it should do what your after.

Selenium xpath gives matches only the visible elements

Selenium xpath gives matches only the visible elements. The HTML page contains lots of other elements which are not visible but present. When trying out the xpath on chrome console, it displays all the elements including the elements which are not visible. But when using the same xpath in selenium, it returns only elements which are visible at that point in time. Is this an expected behavior?
Yes, it's right. I also tried to made reference to elements that weren't in my page (because they were invisible) but I couldn't do that. If the elements are visible in the page you will have access to them.
I recommend you that, if this elements would be visible in some moment, for example, clicking a button, you will have to automatize all the proccess with Selenium and then made reference to them with Xpath, JQuery or whatever you want.
I expect it solves your doubts.

Categories

Resources