How do I get an xpath using Firepath plugin - java

I'm not able to get an xpath using FirePath. I want to locate username field of a login form using xpath in Firepath. But when I try to find the same element,
I'm getting xpath like :
html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input
How can I use it in Selenium script ?

You can use it like:-
WebElement ele1 = driver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input"));
ele1.sendKeys("username");
Here username in double quotes would be replaced by actual value you want to enter in username field.

This is also xpath which is called absolute xpath If you are looking for relative xpath then uncheck the option as shown in below image
And you can use this xpath like :
driver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input")).sendKeys("username");

You can do this via the by.xpath
webdriver.findElement(By.xpath("html/body/div[3]/div/div/div/div/div[2]/div/form/div[1]/input")).click();
You can also search for a relative xpath witch is more robust have a look at this for more information of xpath.
Also have a look at locators in selenium for more information about the diffrent locators

Related

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

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

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

How to deal with Dynamically changing class names in HTML source code while automating using Selenium WebDriver

How to deal with Dynamically changing "class names" or "ids" in HTML source code while automating using Selenium WebDriver
Example Application : GMAIL
if you know the cell index then using XPath or cssSelector will solve the issue. Here in the above screenshot it is clear that the text "Wordpress" is in 5th cell of each table row/so you can use below XPath to get the particular cell.
List<WebElement> cellValue = driver.findElements(By.xpath(".//table/descendant::tr/following-sibling::td[5]"));
The Selenium mechanism for locating elements (org.openqa.selenium.By) offers other methods of locating elements.
Example: name, partialLinkText, css selector, xpath selector, tagname, linkText.
This tutorial from Selenium Easy contains some easy to understand examples.
If you want to locate <span name="WordPress">WordPress</span> element, You can locate this element using By.name() as below :-
driver.findElement(By.name("WordPress"));

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

How to use "And" operator using selenium webdriver with java?

I have a login button and want to identify it using xpath and id. But both the xpath and id are dynamically changing. Please advise on how to do it.
The Xpath and ID keep changing as below for login button and i am not able to find any common element to find it:
Xpath : .//*[#id='uO4Qo']
ID : uO4Qo
Xpath : .//*[#id='tP5Qo']
ID : tP5Qo
Xpath : .//*[#id='vVBPn']
ID : vVBPn
If you're using Selenium IDE to create your script, the target field is a dropdown, displaying all the possible options to identify your specified button. If the options CSS, name or anything else are not available, it means that these things are not present on your page. Perhaps is xpath:position an option in your specific case.
Use below code, use login button text in place of LoginButtonText:
driver.findElement(By.LinkText("LoginButtonText")).click();

Categories

Resources