2017-10-06 34 views
0

我的頁面上有多個包含類似href的按鈕。它們僅與id_invoices不同。我想用XPath和HREF它看起來像點擊頁面上的一個按鈕:按xpath中的最大號碼選擇按鈕

href="/pl/payment/getinvoice/?id_invoices=461" 

我可以使用全部選擇按鈕:

invoices = driver.find_elements_by_xpath("//a[contains(@href, '/payment/getinvoice/')]") 

,但我需要只選擇最高id_invoices按鈕。可以做到嗎? :)

+0

將該值存儲在臨時變量中並與發票相匹配 – iamsankalp89

回答

0

我不知道很多關於蟒蛇所以給你一個方向/算法來實現相同的

Using getAttribute('@href'); 

您將獲得的URL

您需要getText()後,把所有的元素分割字符串你獲得發票List

Split by =並選取最後一個數組值。

現在你需要強制轉換字符串爲int,去年值=之後將有一批

現在你只需要挑選的最高值。

0

你可以做的是:

hrefList = driver.find_elements_by_xpath("//a[contains(@href, '/payment/getinvoice/')]/@href") 

for i in hrefList: 
    hrefList[i]=hrefList[i].split("id_invoices=")[-1] 

max = max(hrefList) 

driver.find_elements_by_xpath("//a[contains(@href, '/payment/getinvoice/?id_invoices="+str(max))+"'"+"]").click() 
0

既然你有一個返回所有需要的元素的XPath,你只需要抓住從每一個href屬性,用「=」獲得分裂HREF id(字符串的第二部分),找到最大的id,然後使用id找到你想要的元素並點擊它。

invoices = driver.find_elements_by_xpath("//a[contains(@href, '/payment/getinvoice/')]") 
ids = [] 
for invoice in invoices 
    ids.append(invoice.get_attribute("href").split('=')[2]) 
results = list(map(int, ids)) // you can't do max on a list of string, you won't get the right answer 
id = max(results) 
driver.find_element_by_xpath("//a[@href='/pl/payment/getinvoice/?id_invoices=" + id + "']").click