簡體   English   中英

Selenium Webdriver,python-單擊頁面上的1st / 2nd / 3rd / etc元素?

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

我為公司的電子商務平台進行軟件測試,並試圖使我們的結帳流程測試自動化。 對於多船選項,每個訂單項都有一個單獨的“添加地址”鏈接。 我可以輕松定位第一個,但是如何定位第二個/第三個/等? 我不是開發人員,所以任何幫助都將非常有幫助。 這是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>

所有地址均為“ editaddress 11”。 不知道是誰決定的:-)

您可以提供的任何幫助都會很棒。 我正在努力學習webdriver。 謝謝!

閱讀文檔 ,我認為函數名稱可以說明一切。

#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

假設我們所知道的只是“添加新地址”標題,我們可以(python示例)創建一個匹配此規則並進行迭代的元素列表。

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

您需要在這里使用xpath選擇器:

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

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

等等等等

你應該做的是

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

希望這對傑克有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM