kroe761
kroe761

Reputation: 3534

Selenium Webdriver, python - click 1st/2nd/3rd/etc element on a page?

I do software testing for my company's e-commerce platform, and am trying to automate our checkout process testing. For multiship option, each line item has a separate "Add an address" link. I can easily target the first one, but how can I target the 2nd/3rd/etc? I am NOT a developer, so any help would be very helpful. This is a snippit of the html for one of those links add an address link -

<div class="editaddress eleven">
    <a class="add dialogify desktop" title="Add New Address" data-dlg-options="{ "width" :    "385px", "dialogClass" : "address-add-edit multishipping", "title" : "Add New Address"}" href="https://XXXXXXXXXXX/COShippingMultiple-EditAddress?i=bcvVIiaagN4ckaaada3w22QH7J"> Add New Address </a>

All of the addresses are "editaddress eleven." Don't know who decided that :-)

Any help you can provide would be wonderful. I am trying my best to learn webdriver. Thanks!

Upvotes: 0

Views: 2825

Answers (3)

Jacob
Jacob

Reputation: 16

you want xpath selectors for this here you go :

//a[@title='Add New Address'][i]

Xpath is a relative selector, the // tells the selector to look anywhere in the document for an "a" type element which is what you have listed, the [@title='Add New Address"] says only return those "a" type elements with a title of "Add New Address". The last part is an index reference number... I am assuming you have more than one Add New Address element, as long as they are similar Elements, replace i with the # of the element you want, relative to its appearance in the document, from the top down.

//a[@title='Add New Address'] - will get you all of the elements
//a[@title='Add New Address'][1] - will get you the first
//a[@title='Add New Address'][2] - will get you the second

so on and so forth

what you should do is

//count number of elements on the page and store them as an integer

int numberofElements = selenium.getXPathCount("//a[@title='Add New Address']")

//loop through the elements, grabbing each one and doing whatever you please with them

for(i=0;i<numberofElements;i++){
  selenium.click("//a[@title='Add New Address']["+i+"]");
  //insert rest of commands you want selenium to do for every one of the elements that are   on the page
 }

hope this helps Jake

Upvotes: 0

Dave_B
Dave_B

Reputation: 18

Assuming all we know is the 'Add New Address' title we could (python example) create a list of elements matching this rule and iterate through it.

lines = driver.find_elements_by_css_selector("a[title='Add New Address']")

Upvotes: 0

thkang
thkang

Reputation: 11543

read the docs, I think the function names speak for themselves.

#variable "driver" is the current selenium webdriver.

div = driver.find_element_by_css_selector('div.editaddress.eleven')

links = div.find_elements_by_css_selector('a.add.dialogify.desktop')

for link in links:
    #do_something

Upvotes: 3

Related Questions