Get XPath from specific content - java

I am trying to parse websites and get the XPath for specific pieces of content.
For example:
On www.stackoverflow.com I want to get the XPath to the 'Questions' button. Using a Chrome extension I find that the following XPath can grab 'Questions':
/html/body[#class='home-page new-topbar']/div[#class='container']/div[#id='header']/div[#id='hmenus']/div[#class='nav mainnavs']/ul/li[1]/a[#id='nav-questions']
Now I want to know is a way to programmatically get the XPath for a given piece of content on a webpage?

Related

Send text to div in Selenium + Java

I want to send a text to a div element in web.whatsapp. I tried several ways ie using XPath, cssSelector, class name etc. but none is working for me.
This textbox comes when we attach an image in web.whatsapp.
Please step into the div and use the div with the class "selectable-text". As this is a contenteditable div you should be able to click into it and call then sendKeys() onto the element.
if this does not help please let me know then I will look in our code how we are filling such divs exactly.
You need to drill down further until you get to a web element that takes an input.

CSS Selector: Anchor text of href contains

I am currently working with Selenium and have now reached the interesting, yet incredibly difficult, world of CSS selectors.
I'm currently looking at selecting the different options of the Google tool bar. E.g when you search for something, on the results page you get options to search for the same term but under images, news, videos etc
I'm particularly interested in selecting the "Images" link.
I've been working on it for quite a while and the closest i have got is the below selector:
div a.q.qs[href]
This drills down into the right sub classes but there are 16 of them. Despite hours of aimless searching, i'm unable to complete the query with a contains method around the anchor text, which is unique in the targeted sub classes.
I'm aware there is a By LinkText option in Selenium, but i'm not sure if the anchor text is unique across the entire page. Also, i really want to understand CSS selectors in general so even if it was, i want to resolve this particular issue so i can apply it to future problems.
I'm looking for something like the below pseudo CSS selector:
div a.q.qs[href].Anchorcontains("Images")
Can anyone help?
All links have a unique parameter called tbm: its value is isch for images, so I'd go with
a[href*="tbm=isch"]
There are sometimes ways to get what you want with CSS selectors but if you want to find an element by contained text, you will have to use either link text/partial link text if it's a link or XPath for everything else.
The XPath for what you want is
//div[#id='hdtb-msb-vis']//a[.='Images']
You could use //a[.='Images'] but that returns two elements, one of which is not visible.
To break this down
// at any level
div find a DIV
[#id='hdtb-msb-vis'] that contains an ID of 'hdtb-msb-vis'
//a that has a child A at any level
[.='Images'] that contains text (.) equal to 'Images'
If you want to explore by link text, you can write something like
int count = driver.findElements(By.linkText("Images")).size();
and print count. My guess is that it will be 2, one of which is not visible. You can use Selenium to further filter this down to only the visible link, if you wanted.
You are going to have the same issue with BackSlash's CSS selector answer. You can tweak it slightly and solve this problem with the CSS selector locator
#hdtb-msb-vis a[href*='tbm=isch']
Here are some links to CSS references that will help you get started: W3C reference, SauceLabs CSS Selectors tips, and Taming Advanced CSS Selectors.

How to identify an element in selenium even if xpath is same for all the matching nodes

I am new to selenium and trying to automate a web application in junit framework. As many get some problem in identifying web elements, I am stuck at a point where two submit buttons are having same xpath
//img[#src='https://blrwvmmstst3:8643/madmin/images/icons/add.png']
you can get both with getElements instead of getElement method
or, and thats what id do, try changing the xpath in a way that only the desired submitbutton is returned, for example you could check the buttons id or text

Screen scraping, extract text data from DOM tree

I'm trying to scrape specific text from a website using jsoup. The text doesn't appear to be in the html that I'm downloading, but rather in the DOM tree. Im not exactly sure how this works because I'm new at it. When I view the source of the website I'm unable to see or find the text that I'm looking for, but I can find it in the DOM tree. How can I extract the data?

Unable to retrieve Textbox Value In Selenium Testing

I am new to Selenium testing, I am trying to put the value into the textbox with the help of xpath, name & id. (java using eclipse IDE):
driver.findElement(By.xpath(".//* [#id='txtEmpDepAddressLine1']")).sendKeys("2nd Main")
driver.findElement(By.xpath(".//*[#id='txtEmpDepAddressLine2']")).sendKeys("Bangalore");
While running, textbox is not retrieve the from the above code. Can anyone help me out to solve this problem?
Use By.id instead By.xpath
If you insist xpath then:
Start with: // instead: .//
Make sure the id value is unique on page.
if using xpath in some browsers id or ID makes differnce.
It is difficult to say without the HTML, but I'd suggest a few things:
Open your page, go to the Google Chrome DevTools console, and verify you can locate the element using your xpath. So you'd type:
$x("//* [#id='txtEmpDepAddressLine1']")
and verify the text box is located.
If the sendKeys isn't working on the element at that id, I'd wonder if it is not on the text field itself, but on a containing table or something like that. Make sure the element located by the id is the input, so the sendKeys will work.
By
In the text box it is fetching the value but it can't able not store
in the text box field
do you mean you can see selenium typing those values into the textbox and then the values disappear?
If that is what is happening, it is possible there is some javascript running on the page while selenium is entering the values in the textbox. you can try to put a wait before selenium enters the value. If waiting resolves the issue, you will need to write a method to detect when the javascript is done.

Categories