selenium ide having problems with dynamic id - selenium

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

Related

Not able to click or page not responding to click in phantomjs-selenium in java

I am doing project on different crawlers and trying to mimic user actions. As part of it, I am crawling this url. Here there is a zip code box and I am trying to click on it and extract text from the drop down which will appear after that. I wrote the below code but not sure why it is not working at all. Can anyone please help? I did exhaustive search to find root cause but got nothing. Any suggestions would be much much appreciated.
driver.getUrl(aboveUrl);
WebElement inputBox = driver.findElement(By.id("pincodeInputId"));
inputBox.click();
System.out.println(driver.findElement(By.className("_3mWImx")).getText());
-- This gives null;
Awaiting help !
The reason is the node that you picked is the parent node of the element that has the text
You should use
System.out.println(driver.findElement(By.css("_3mWImx span")).getText());
And that would work. Also note that there are multiple element with the class _3mWImx, so this will only give you the first one. If you are interested in all of them, then you should be using driver.findElements and looping through the result
Actually there are more than one values in the drop down if you want to print all you have to used findElements(). Use this code it will give you desired result :
WebDriver driver=new FirefoxDriver( );
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/moto-e4-plus-fine-gold-32-gb/p/itmevqynuz4fwxca");
WebElement inputBox = driver.findElement(By.id("pincodeInputId"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
inputBox.click();
List<WebElement> elements=driver.findElements(By.className("_3mWImx"));
for(WebElement ele:elements)
{
System.out.println(ele.getText()); //It will print innertext of each element
}
Output :
From Saved Addresses
Loginto see your saved addresses

Is there a way to click plain text in Codeception acceptance tests without using XPath?

Using Codeception acceptance test (with WebDriver), I would like to know if there is a way to click an element that contains a specific text, without that element being a link or a button. I know it can be done using XPath, but I'm looking for a more readable solution that uses CSS-selectors for example.
Without specific examples, probably the best you could do is to look for a group of elements using a CSS selector then loop through that collection looking for contained text. Here's a contrived example where I'm looking for a TD that contains the text "Click here".
List<WebElement> cells = driver.findElements(By.cssSelector("td.someclass"));
for (WebElement cell : cells)
{
if (cell.getText().contains("Click here"))
{
cell.click();
break; // found it, don't need to keep looping
}
}
If you want your search to look for the text, then XPath is your only option.

Xpaths are changing dynamically

Im currently auotmating an web application using a selenium framework.For locating the elements im using xpath. For particular drop downs(with a filter in it) in the application the xpaths are getting changed frequently(for the available options in the drop down).The options in the drop down are inside the span section. Is there are any way to handle the dynamic xpath? Currently im using firebug to get the xpaths.
You can always use XPATH text matching if you know what to expect.
//*[text()='DropdownValue']
if you have list elements in the dropdown:
//li[text()='DropdownValue']
If you could provide example of the dropdown structure or just the HTML code of your case, it would be much easier to find a suitable solution.
You could get every element inside your dropdown, get the index of the element which contains the text and then click it:
List<WebElement>elems = driver.findElements(By.xpath("xpath from every element"));
elems.get(getIndex(elems, "Abereen District")).click();
private int getIndex(List<WebElement>elems, String elemText) {
for(int i = 0; i < elems.size(); i++)
{
if(elems.get(i).getText().contains(elemText))
return i;
}
return -1;
}

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

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