How to handle the button click in Selenium Webdrive? - selenium

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;
}

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

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

Selenium preceding-sibling::text() not working

I am having issues with selenium using the next xpath "./preceding-sibling::text()"
I don't understand why, my first thought was that IE wasn't supporting that xpath statement but it didnt work on chrome neither.
What I am trying to do is to verify if a radio button have a certain text "near" it. For example if a radio button is like this
<div>
<label> Yes <input name='radioBtn'></input></label>
<label> No <input name='radioBtn'></input></label>
</div>
This is a simplified scenario where I need to check the "yes" radio button, so what I am doing is to search for the radiobutton and check if it preceding-sibling::text(), but selenium is cant find any element. I know the Xpath works because I test it on chrome developer tools and it returns the text "Yes".
I can't do something like 'input[1]' because I can't be sure that the first one will be always that have the Yes text.
Any idea why isn't this working on selenium? and if there is any work around?
I got to a work around but is kind of specific to the problem. But let's answer the questions 1 at the time.
Any idea why isn't this working on selenium?
It's not working because selenium don't support text elements, so even when selenium find the element it cant map it to a selenium object, i didn't see it because my code hided the selenium exception. The Exception throw is the next one:
An unhandled exception of type
'OpenQA.Selenium.InvalidSelectorException' occurred in WebDriver.dll
Additional information: invalid selector: The result of the xpath
expression "./preceding-sibling::text()" is: [object Text]. It should
be an element
Is there any work around?
Yes it is. What I did was to run a java script using the selenium IJavaScriptExecutor. With the script I revised the preceding sibling text and return it as a string so if the text was equal to the thing I was looking for (i.e Yes) trhat means that is the radio button I was looking for.
The code looks is similar to this (it can have some sintax errors since I didn't copied from the source):
string script = "function GetPrecedingText(node){"+
"var result = document.evaluate('./preceding-sibling::text()[1]', node, null, XPathResult.STRING_TYPE, null);"+
"return (result==null) ? '' : result.stringValue.trim();}"+
"return GetPrecedingText(arguments[0])";
string result = ((driver as IJavaScriptExecutor).ExecuteScript(script, SeleniumElement)).toString();
Hope someone can find this useful :) and thanks for all that tried to help me :)

selenium ide having problems with dynamic id

i am testing our web based application using selenium. I am having problem in a button which has dynamic id and the class is similar to the last html page so i am unable to go ahead with the testing. below is the source of the button
input id="aui_3_4_0_1_554" class="addto_cart_button" type="button" onclick="chkMaxRequestPerDay();" value="Request Quote">
I want to know how can i tell selenium ide to check with value so that it can proceed
Thanks
you can try using xpath with value,
//input[#value='Request Quote']
or
//input[#value='Request Quote' and #class='addto_cart_button']
I think something like below should also work
//input[contains(#id, 'aui_')]
or
//input[#class='addto_cart_button']
this will give you a List
loop through them and in the loop check
loop over List<Webelement> {
if( webelement.getAttribute("onclick").indexOf("chkMaxRequestPerDay") != -1) {
// here is the element. do what ever you want
}
}