简体   繁体   English

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

[英]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 - 这是html的摘录,其中一个链接添加了地址链接-

<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." 所有地址均为“ editaddress 11”。 Don't know who decided that :-) 不知道是谁决定的:-)

Any help you can provide would be wonderful. 您可以提供的任何帮助都会很棒。 I am trying my best to learn webdriver. 我正在努力学习webdriver。 Thanks! 谢谢!

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

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. 假设我们所知道的只是“添加新地址”标题,我们可以(python示例)创建一个匹配此规则并进行迭代的元素列表。

lines = driver.find_elements_by_css_selector("a[title='Add New Address']") 行= driver.find_elements_by_css_selector(“ a [title ='添加新地址']”)

you want xpath selectors for this here you go : 您需要在这里使用xpath选择器:

//a[@title='Add New Address'][i] // a [@ title ='添加新地址'] [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. Xpath是一个相对选择器,//告诉选择器在文档中的任何位置查找您所列出的“ a”类型元素,[@ title ='添加新地址”]表示仅返回那些“ a”键入标题为“添加新地址”的元素。最后一部分是索引参考号...我假设您有多个添加新地址元素,只要它们是相似的元素,请将i替换为#相对于其在文档中的外观,从上至下所需的元素。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python numpy的二维数组中将位置(第2、第3或第4等)转换为索引(第1位置为00,第2位置为01等)? - how can I convert position (2nd, 3rd or 4th etc ) to index(00 for 1st position, 01 for 2nd etc) in 2D array in python numpy? 使用第一个元素的值获取结果集中第二个和第三个元素的值 - get values of 2nd and 3rd element of a result set using the value of the 1st element 如何在 Python 的列表中找到第一个、第二个、第三个最高值 - How to find the 1st, 2nd, 3rd highest values in a list in Python Python:如何查找模式(第一列 - 对象),按第二列分组,输出到第三列 - Python: How to find mode (1st col - object), grouped by 2nd col, output into 3rd col Python处理日期格式,例如“ 1、2、3、4” - Python deal with date format like “1st, 2nd, 3rd, 4th” 使用矢量化(更好)或其他方法比较多个熊猫列(第一和第二,第三和第四之后,等等) - Compare multiple pandas columns (1st and 2nd, after 3rd and 4rth, after etc) with vectorization (better) or other method 如何在selenium webdriver Python中用鼠标点击第二层元素? - How to click on the 2nd layer of element with mouse in selenium webdriver Python? 如何在Python中按升序对第一个变量,按自定义顺序对第二个变量以及对降序中的第三个变量对DataFrame进行排序 - How can I sort DataFrame in Python on 1st variable in ascending, on 2nd variable in custom order and on 3rd variable in Descending [Kotlin/Python]我可以合并两个映射,所以第三个映射的键是第一个映射的值,第三个映射的值是第二个映射的值吗? - [Kotlin/Python]Can I merge two maps so the key of the 3rd map is the value of the 1st map and the value of the 3rd map is the value of the 2nd map? Python和Selenium / XING:find_element第一次工作,而不是第二次迭代 - Python & Selenium / XING: find_element works 1st time, not 2nd on iteration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM