2017-10-06 72 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