unable to find the element in my application using selenium webdriver - selenium

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

Related

How to find webelement using style property

Below is the HTML code of an element and I want to locate this element by classs and style property using selenium webDriver in java
<div class="qooxdoo-table-cell" style="left:252px;width:117px;height:24px;"/>
suggest a way which can be help full in selenium
I want to locate the element using java code
i.e. Driver.findelement(by. ....
As long as the element isn't unique you must grab both attributes:
This is the general form, replacing the empty strings for your required class and style:
driver.findElement("By.xpath(//div[#class='' and style='']");
So:
driver.findElement(By.xpath("//div[#class='qooxdoo-table-cell' and style='left:252px;width:117px;height:24px;']");
Best of luck!
If you need to match <div> with exact style attribute, you can try something like
driver.findElement(By.xpath("//div[#class='qooxdoo-table-cell'][#style='left:252px;width:117px;height:24px;']"))
Another way is to use cssSelector as follows:
driver.findElement(By.cssSelector("div[style='left:252px;width:117px;height:24px;']"));
If you are using a older version of selenium you can as well use the older version of cssSelector.
driver.find_elements_by_css_selector("div[style='left:252px;width:117px;height:24px;']")
Note: this function still works in current version of Selenium (with DeprecationWarning )

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..:)

How to handle the button click in Selenium Webdrive?

I am new to WebDriver,and currently trying to write the code to click the button. The locator is not available so I have used Xpath,but it is not working as it should be. Kindly help me on this.
Button tag:
<button onclick="myFunction()">Try it</button>
My web drive code:
drive_url.findElement(By.xpath("html/body/button")).click();
Did you check your xpath in browser console. You can check xpath by writing $x("<your xpath>") in console. Try using "//button" instead of what you're using now.
You should be little careful writng the selector as well. Try to avoid flaky selector and make is as unique as possible.
By xpath = By.xpath("//button[contains(text(),'Try it')]");
drive_url.findElement(xpath ).click();
The above selector finds the button tag explicitly using text based search.
Try this:
WebElement btn = driver.findElement(By.tagName("button"));
String btnText= driver.findElement(By.tagName("button")).getText();
if(btnText.equals("Try it")){
btn.click;
}

Not able to identify a lenghty linktext in selenium webdriver

I want to identify an element by linktext, I am facing strange issue
If the linktext value is short i.e addFirst, addLast will be able to locate the element by using
driver.findelement(By.linktext("addLast, addFirst")).click
IF the linktext is addmanualreferraltocheckthelenghtF, addmanualreferraltocheckthelenghtF lengthy as above not able to identify the element
Please help me to find the solution
Why don't you try partial link text.
driver.findelement(By.partialLinkText("addLast, addFirst")).click
try out contains method. use the XPATH value and in XPATH use contains for link text.
EG:
.//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]
driver.findelement(By.xpath("//*[contains(#Linktext, "addmanualreferraltocheckthelenghtF")]").click
Alternatively you can also use starts with XPATH value.
.//*[starts-with(#linktext,'addmanual')]
driver.findelement(By.xpath("
//*[starts-with(#linktext,'addmanual')]").click
Hope it helps you.

Xpath for href element

I need to click on the below href element,which is present among similar href elements.
<a id="oldcontent" href="listDetails.do?camp=1865"><u>Re-Call</u></a>
Can anyone provide me xpath to click the above href link?
Try below locator.
selenium.click("css=a[href*='listDetails.do'][id='oldcontent']");
or
selenium.click("xpath=//a[contains(#href,'listDetails.do') and #id='oldcontent']");
This works properly try this code-
selenium.click("xpath=//a[contains(#href,'listDetails.do') and #id='oldcontent']");
This will get you the generic link:
selenium.FindElement(By.XPath("xpath=//a[contains(#href,'listDetails.do')")).Click();
If you want to have it specify a parameter then you will have to test for each one:
...
int i = 1;
selenium.FindElement(By.XPath("xpath=//a[contains(#href,'listDetails.do?camp=" + i.ToString() + "')")).Click();
...
The above could utilize a for loop which navigates to and from each camp numbers' page, which could verify a static list of camps.
Please excuse if the code is not perfect, I have not tested myself.
have you tried:
//a[#id='oldcontent']/u[text()='Re-Call']
for me worked //a[text()='Re-Call']
what worked for me:
//a[contains(#href,'logout')]
Below works fine.
//a[#id='oldcontent']
If you've tried certain ones and they haven't worked, then let us know, otherwise something simple like this should work.
Best way to locate anchor elements is to use link=Re-Call:
selenium.click("link=Re-Call");
It will work..