简体   繁体   English

如何使用硒获取“ href”链接?

[英]How can get 'href' link using selenium?

I using selenium-python. 我用的是硒蟒蛇。 and I want to make ' E-mail verify case '. 我想做一个“ 电子邮件验证案例 ”。 In the test case, I forwarding ' a href link. 在测试案例中,我转发了一个href链接。

Yes. 是。 It case, just using find_element_by_partial_link_text i know. 在这种情况下,仅使用我知道的find_element_by_partial_link_text But I do not understand isn't work in my case. 但是我不明白在我的情况下是行不通的。

here is case 这是案例

<tbody> 
<tr>
  <td style = "padding-bottom:40px;">
     <a href="(auth link)" target="blank" style="(some style)">Verify address</a>
  </td>
</tr>
</tbody>

I am using 我在用

  1. find_element_by_partial_link_text("Verify") / ("Verify Address") / ("address")
  2. find_element_by_css_selector("padding-bottom").get_attribute('href')

but there are make ' selenium.common.exceptions.NoSuchElementException ' exception : < 但是有selenium.common.exceptions.NoSuchElementException '例外:<

and Try <br> find_element_by_xpath() , but that auth-link is changing every time. 并尝试<br> find_element_by_xpath() ,但是auth-link每次都在变化。

How to get to href link and click this link? 如何获得href链接并单击此链接?

try to introduce webdriverwait. 尝试介绍webdriverwait。

I'm changing link_text to partial_link_text , Just make sure only one Verify address link should be present in the UI, otherwise code will not throw any error, It'd just pick the first element. 我将link_text更改为partial_link_text ,只需确保UI中仅存在一个Verify address链接,否则代码将不会引发任何错误,只需选择第一个元素即可。

wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, 'Verify address')))

For clicking just use : 对于单击,只需使用:

element.click()

For getting href , use : 要获取href ,请使用:

element.get_attribute('href')

You can follow this official Link for more. 您可以关注此官方链接以获取更多信息。

You need to import these : 您需要导入这些:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 

Let me know, If you have any more concerns. 让我知道,如果您还有其他顾虑。

You can also try XPATH using text. 您也可以使用文本尝试XPATH。 Try any one from the following list: 尝试以下列表中的任何一个:

//a[text()='Verify address']
//a[contains(text(),'Verify address')]
//a[starts-with(text(),'Verify')]

After find element you can get_attributes or any other action. 在find元素之后,您可以get_attributes或任何其他操作。

Hope it will solve your problem. 希望它能解决您的问题。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM