Can someone throw some light on how to dynamically retrieve List of Id by PageSection.
Our Page Contains number of sections based on number of person information.each person is a Form based PageSection.
Here is an example.
#FindBy(css = "#personForm0 > fieldset > div.apisInfoRequired.statusBox.active > div.btn.action.addBtn > input")
public PageElement addInfantBtn;
#FindBy(css = "#personForm1 > fieldset > div.apisInfoRequired.statusBox.active > div.btn.action.addBtn > input")
public PageElement addAdultBtn;
#personForm0 can be go upto #personForm9, now when i read that using #FindBy, I am facing difficulty with selenium. Can someone share your thought on how to write this.
Are you able to find all elements that has a specific name or similar and the iterate over them and determine if you have found what are are searching for?
That is, instead of using
findElement(By.name("q"));
are you able to use
findElements(By.name("q"));
and iterate on the list of web elements that are returned?
An example of the html you are working with would probably give you a better answer.
Related
I have a Java project using Selenium and Page Object Model and need to find buttons with IDs that end with the String "Cancel". I tried using regular expressions also, I found a few solutions on stackoverflow that included XPath, but taking into consideration that the website's design is changing often I do not use XPath.
I also found as a solution that you can use an ends-with CSS selector:
By.cssSelector("[id$=default-create-firstname]") but I would like to take advantage of the Page Object Model and use the annotation #FindBy, therefore omitting the By selectors.
#FindBy(id = "ButtonToCancel")
private WebElement buttonToCancel;
How can I select all the IDs in the page that end in *Cancel, without hardcoding each id find #FindBy? From what I know, Regex patterns do not work as such: #FindBy(id="*Cancel")
Try this:
#FindBy(css = "*[id$='cancel']"
private List<WebElement> cancelButtons;
It will return list of WebElements with id ended with "cancel" in id field.
enter image description hereI am using the latest version of selenium web driver, for a responsive web page is there any way I can use single webelement(as a list webelement) to access row data.
For example: following CSS in browser
iPad resolution(1st div in html):
li > .csd-aabc-item > .terraVM-ResponsiveModel-container .terraVM-TableCard--truncated
iPhone resolution(2nd div in html) :
li > .csd-aabc-item > .terraVM-ResponsiveModel-container .terraVM-CompactCard
if I am using #FindAll, getting list size for both locators but I need only for one.
You can make a partial class check - for instance, check that there is a class that starts with terraVM and contains Card:
li > .csd-aabc-item > .terraVM-ResponsiveModel-container [class^=terraVM][class*=Card]
^= means "starts with", *= means "contains".
Not sure how unique of a locator that would be on your page, but answering based on what was provided in the question.
I am trying to select a value(Bellevue) from a li(it looks like a dropdown but it isn't).The problem is that its id changes everytime the page loads.
Here is a screenshot:
This time the id is: ui-id-23,but the number,23,will be changed next time so this will not work.If I expand the <a id="ui-id-23..." I get the name 'Bellevue' but every character surrounded by < strong > < /strong > mark-up.
I can't find it after it's classname because both values from li have the same class,ui-menu-item.
I tried after xpath:"//a[contains(text(),'Bellevue')]" but I get the error:Unable to locate element...
Do you know any solution for this?I am using Selenium Webdriver in Java and TestNG.
Thanks!
Update
So I managed to find that element by using:
WebElement value = driver.findElements(By.cssSelector("a[id^='ui-id-']")).get(3);
value.click(); .
but in my application i am using page objects and i look after elements using #FindBy(how.HOW.....).Do you know how I can use .get(3) with #FindBy?
You want to use a CSS selector on the ID:
a[id^='ui-id-']
This says "Find all of the a elements that have an ID that start with ui-id-"
If you want to find the second item, then do:
driver.findElements(By.cssSelector("a[id^='ui-id-']"))[1]
The [1] will select the second item on the page.
It looks like jQuery uniquId() method is used to populated the id, so it will always start with ui-id-. You can use jQuery selector to select element whose id starts with ui-id-
WebElement webElement = (WebElement) ((JavascriptExecutor) webDriver).executeScript("return $( 'input[id^="ui-id-"]').get(0);");
I would try to use xpath avoiding using of id. For example, //a[#class=''ui-corner-all ui-state-focus ][2]
First get the tag name in which your id attribute has been defined.
WebElement ele = driver.findElement(By.tagName(tagName));
String strId = ele.getAttribute("id").startsWith("ui-id-");
driver.findElement(By.id(strId)).click();
I am trying to interact with the Nike shoe online shop, login and then select a shoe size from the list and then press add to cart button from Selenium's Java WebDriver:
http://store.nike.com/us/en_us/pd/air-max-2014-running-shoe/pid-830258/pgid-774364
First of all I cannot seem to find the correct <li> element and select it - and need some advise on how to do it.
I am finding my code does not work for selecting a shoe size : pastebin.com/6K1RpPKL (as guided by the kind user on the first response.
The element type in li is not select. Use following code instead, it will work fine.
WebElement shoeSizes = driver.findElement(By.xpath("//div[contains(#class,'exp-pdp-size-container')]/a"));
shoeSizes.click(); // Expanded
String shoeSize = "8.5";
WebElement shoeSizeSel = driver.findElement(By.xpath("//li[text()='"+shoeSize+"']"));
shoeSizeSel.click(); // Size selected
driver.findElement(By.xpath("//div[#class='exp-pdp-save-container']/button")).click(); // Added to cart
As far as advice goes, you should first learn the basics like identifying elements, using locators before asking these kind of question. Go through these: Selenium Docs, Mozilla Blogs. Many such resources are available on web.
First off, you should get away from the IDE if you are wanting more robust test-writing like flash. For your login issue, this is simple.
Using the getting started with selenium framework your test would look like:
#Config(url="http://store.nike.com/us/en_us/pd/air-max-2014-running-shoe/pid-830258/pgid-774364")
public class NikeTest extends AutomationTest {
#Test
public void testNike() {
click (By.cssSelector("div.login.nav-section > a"))
.setText(By.cssSelector("form[name='login-form] input[name='email']"), "<My Username>")
.setText(By.cssSelector("form[name='login-form] input[name='password']"), "<My Password>")
.click (By.cssSelector("form[name='login-form] button.exp-login-submit")
// now we're logged in.
// let's select a size of shoe.
.click (By.cssSelector("div.exp-pdp-size-and-quantity-container > a.exp-pdp-size-dropdown") // now it's expanded.
.selectOptionByText(By.cssSelector("select[name='skuAndSize']"), "10.5") // you can replace 10.5 with whatever option you need.
}
}
Those are some CSS selectors you can use. Also per your Flash thing, i think you're out of luck buddy.. I hadn't heard of any very successful solution with automating flash.
One key thing here:
Make sure you know what element is receiving the click. Selenium IDE doesn't do a great job determining WHAT exact element is receiving the click. My guess is that it was trying either the <div> or <li> when it's the <a> that actually makes the dropdown come down.
Just for learning purpose, I am trying to click on the third element of the Google Results Suggestions
In the above picture, i want to click on qubool hai. My code gets the result suggestions and clicks the 3rd element.
List<WebElement> resultsuggestion = driver.findElements(By.cssSelector(".gssb_m > tbody:nth-child(1) > tr"));
new Actions(driver).click(resultsuggestion.get(2));
But Selenium doesn't click on it. Kindly let me know if anything wrong in the above code or suggest me alternative solutions
Try changing your code to:
WebElement result = driver.findElement(By.cssSelector(".gssb_m > tbody > tr:nth-child(3)"));
result.click();
using the :nth-child typically is necessary for specifically identifying children. You seem to be trying to find multiples of only 1 tbody.
furthermore, using the Actions class for a simple click is very unnecessary when you have the WebElement#click method.
Following is another way I found:
WebElement result = driver.findElement(By.cssSelector(".gssb_m tr:nth-of-type(3)"));
result.click();
.gssb_m tr:nth-of-type(3) : Under class='gssb_m' element, it looks for third tr tag.