in selenium xpath and innerHTML - selenium

I am new to selenium. I want to automate the select option present on my webpage. I am trying to use select with xpath. Is it possible to get the innerHTML without having id but only with xpath?
If yes how ? If no then how to solve the issue with select.

Yes, it is possible. Start here: http://www.w3schools.com/xpath/.
And here's a quick dropdown example in python:
from selenium.webdriver import Chrome
SETTINGS_PAGE_URL = 'chrome://settings/browser'
SEARCH_ENGINE_DROPDOWN_ID = 'defaultSearchEngine'
SEARCH_ENGINE_CHOICE_XPATH = '//option[text()="Google"]'
browser = Chrome()
browser.get(SETTINGS_PAGE_URL)
dropdown = browser.find_element_by_id(SEARCH_ENGINE_DROPDOWN_ID)
option = dropdown.find_element_by_xpath(SEARCH_ENGINE_CHOICE_XPATH)
option.click()

Anyways - without the HTML code of the page, I can give you only general advice about XPath. See this page: http://zvon.org/xxl/XPathTutorial/Output/example1.html
It helped me a lot understanding the XPath approach

Related

unable to find the element in my application using selenium webdriver

HTML code for element is:
<select id="pt1:reg2:1:soc1::content" class="x2h" title="" theme="dark" name="pt1:reg2:1:soc1">
Every time the xpath is getting changed but i am unable to find element.
WebElement w1 = driver.findElement(By.xpath(".//select[starts-with(#id, 'pt1') AND contains(#id, ':soc1::content')]"));
Select s = new Select(w1);
s.selectByVisibleText("Commercial");
I tested the xpath with a online tool like: http://www.xpathtester.com/xpath
This query worked just fine, i changed the and to lowercase.
.//select[starts-with(#id, 'pt1') and contains(#id, ':soc1::content')]
Write Xpath in Double quotes "xpath here" and
Don't take first full stop '.' from xpath,its of no use.
It start with //
Good luck
".//select[starts-with(#id, 'pt1') AND contains(#id, ':soc1::content')]"
Here full stop(.) before // need to be removed in Xpath.
Below one is without "AND" which is also working fine.
//select[starts-with(#id, 'pt1')][contains(#id,':soc1::content')]

Selenium Webdriver: cssselector

I am trying to do SignIn for this. In it click on 'SignIn' which I have done it successfully. Now when trying to type in Username/Password using Xpath it shows exception which says
Exception in thread "main"
org.openqa.selenium.ElementNotVisibleException: element not visible
code is :-
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[1]/input")).sendKeys("test123");
driver.findElement(By.xpath(".//*[#id='load_form']/fieldset[2]/input")).sendKeys("test123");
I know this Xpath is same as used in SignUp form, so what are the other ways to locate these two fields? How we can use it by using cssselector?
Thanks for the help in advance.
Go One Level up on finding the relative xpath with
//div[#id='login']/form[#id='load_form']//input[#name='username']
//div[#id='login']/form[#id='load_form']//input[#name='password']
Try this
//For Username
driver.findElement(By.xpath("(//input[#name='username'])[2]")).sendKeys("username test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='usernam']")).click();
//For password
driver.findElement(By.xpath("(//input[#name='password'])[2]")).sendKeys("password test");
//or
driver.findElement(By.cssSelector("#login > #load_form > fieldset > input[name='password']")).click();
the above codes are working for me.
There are basically two elements found by provided xpath, and it works with first found element which is invisible as your exception saying, you should try using cssSelector as below :-
driver.findElement(By.cssSelector("div#login input[name = 'username']")).sendKeys("test123");
driver.findElement(By.cssSelector("div#login input[name = 'password']")).sendKeys("test123");
Note:- For learning about cssSelector follow this url and for xPath follow this url
I'm suggesting you before selenium execution with the locator you need to sure that using locator is correct and returns single or multiple element at your browser console by pressing f12. for verify xPath you should use $x("your xpath expression") and for cssSelector you should use document.querySelector("your css selector expression")..
Hope it will help you..:)

Selenium automation with xpath for all browsers

I am new to selenium automation and I am trying to figure out how to handle the code for an element that shows different xpath for different browsers.
//xpath for IE
private readonly By ByPermIE = By.XPath("//*[#id='group-permissions']/div[1]/div[2]/span[2]");
//xpath for chrome
private readonly By ByPermChrome = By.XPath("//*[#id='group-permissions']/div[1]/div[1]/span[2]");
If I am performing some action on this element on different browsers how do I use these elements in the test case. I am planning to extend this to three more browsers. So, should I use if else conditions everywhere? Is there any alternate of best practice for such cases?
You can use below xpath for you case.
private readonly By ByPermChrome = By.XPath("//*[#id='group-permissions']/div[1]//span[text() = 'text that span contains']");
You can write xpath as relative as possible.It is not advisable to write absolute xpaths.
For your above case use below xpath which works in both the browsers.
By.XPath("//*[#id='group-permissions']//span[2]");
or
By.XPath("//*[#id='group-permissions']//span[text()='someThing']");
Refer this for more info regarding xpath.
if u post ur html code, than it will be easy. But as there is no html given, i can suggest u a way like below:
//u can use if condition here
if(driver.findElements(By.XPath("//*[#id='group-permissions']/div[1]/div[2]/span[2]")).size()==1){
//perform operation here
}
else
//perform operation here
This is an alternative way as there is no html code.
If u give the html code, i think we can suggest u more be
One suggestion I can give here is you can use OR conditions in xpaths. Say OR OR .
Xpath OR returns successful as soon it hits the first correct one.

Selinium web driver- Unable to locate href

I trying to locate a link "Taschen" in the below code(<a data-flyout-target="handbags" title="Taschen" href="#">Taschen</a>) using selinium webdriver. Could anyone please guide me
<a data-flyout-target="handbags" title="Taschen" href="#">Taschen</a>
Here are your choices:
By.className
By.cssSelector
By.id
By.linkText
By.name
By.partialLinkText
By.tagName
By.xpath
So assuming that "Taschen" is partial link text
WebElement aLink = driver.findElement(By.partialLinkText("Taschen"));
If you haven't bothered with page loads and proper implicit waits then this might work better
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement aLink = wait.until(
ExpectedConditions.elementToBeClickable(By.partialLinkText("Taschen"))
);
aLink.click();
You are not using java? Update your question to show your code and add a language tag to your question. Your brain isn't a network connected device and we only know what you tell us. :-)
You can also use the XPath as below:-
//a[#title='Taschen']/text()
Code will be as below:-
WebElement tash = driver.findElement(By.xpath("Taschen"));
If you need to get the text then you can use :-
String tash = driver.findElement(By.xpath("Taschen")).getText();
You can also use the contains function in XPath
//a[contains(.,'Taschen')]
Hope it will help you :)

How to find the exact xpath of an element (link) in an webpage

Initially I tried with below code:
WebElement admin = driver.findElement(By.xpath("//a[text()='Users']"));
and then also with
WebElement usrs = driver.findElement(By.xpath("//html/body/div[2]/div[3]/div[1]/form/div[4]/div[2]/div[1]/a[linktext()='Users']"));
The above codes didn't identify the object (link) in web page.
Please help me to resolve this issue.!
Try for the first 'a' element containing 'Users'.
WebElement admin = driver.findElement(By.xpath("//a[contains(text),'Users']"));
or for exact match
WebElement admin = driver.findElement(By.xpath("//a[.='Users']"));
and
"//body/div[2]/div[3]/div[1]/form/div[4]/div[2]/div[1]/a[contains(text),'Users']"
If you're going the xpath route, make it easy on yourself. Inspect element with Chrome then right click on the element in the chrome console and "copy xpath". That will give you the exact xpath and you don't have to worry about any "contains" or anything else. That's what the [1] is for.