[英]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.