Selenium Webdriver,python - 单击页面上的1st / 2nd / 3rd / etc元素?

时间:2013-08-02 14:39:10

标签: python selenium webdriver

我为公司的电子商务平台进行软件测试,并尝试自动化结帐流程测试。对于multiship选项,每个订单项都有一个单独的“添加地址”链接。我可以轻松定位第一个,但我如何定位第二个/第三个/等?我不是开发人员,所以任何帮助都会非常有帮助。这是html的一个snippit,其中一个链接添加了一个地址链接 -

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

所有地址都是“editaddress 11”。不知道是谁决定: - )

您可以提供任何帮助都很精彩。我正在努力学习webdriver。谢谢!

3 个答案:

答案 0 :(得分:3)

阅读docs,我认为功能名称不言自明。

#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

答案 1 :(得分:0)

假设我们所知道的是“添加新地址”标题,我们可以(python示例)创建一个符合此规则的元素列表并迭代它。

lines = driver.find_elements_by_css_selector(“a [title ='添加新地址']”)

答案 2 :(得分:0)

你想要xpath选择器 你走了:

// a [@ title ='添加新地址'] [i]

Xpath是一个相对选择器,//告诉选择器在文档中的任何位置查找你所列出的“a”类型元素,[@ title ='Add New Address']表示只返回那些标题为“添加新地址”的“a”类型元素。最后一部分是索引引用号...我假设您有多个Add New Address元素,只要它们是类似的元素,替换为你想要的元素的#,相对于它在文档中的外观,从上到下。

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

等等等等

你应该做的是

//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
 }
希望这会有所帮助 杰克

相关问题