简体   繁体   English

如何使用 Selenium 和 Python 单击 href 链接

[英]How to click on href link using Selenium and Python

I am working on a program that automates logs into to a certain webpage and clicks certain buttons & links to reach a final destination to enter certain values and submit them.我正在开发一个程序,该程序可以自动登录到某个网页并单击某些按钮和链接以到达最终目的地以输入某些值并提交它们。 I have managed to navigate through the webpages but one of the webpages has a hyperlink button that I need Selenium to click, however, after trying multiple different methods, I cannot get it to work.我设法浏览了这些网页,但是其中一个网页有一个超链接按钮,我需要 Selenium 才能单击它,但是,在尝试了多种不同的方法后,我无法让它工作。

I have tried finding the element with By.XPATH , By.LINK_TEXT , By.PARTIAL_LINK_TEXT and none of these worked.我尝试使用By.XPATHBy.LINK_TEXTBy.PARTIAL_LINK_TEXT找到元素,但这些都不起作用。 I thought my issue might be that since it is clicking onto a totally new URL, so I load the new URL towards the bottom of my code to then move forward with my program.我想我的问题可能是因为它点击了一个全新的 URL,所以我将新的 URL 加载到我的代码底部,然后继续我的程序。

The hyperlink button: Button超链接按钮: Button

The chunk of code to the hyperlink button I am trying to click on:我试图点击的超链接按钮的代码块:

包含超链接的代码

The XPath itself is: /html/body/div[2]/table/tbody/tr/td[2]/p/span/a[2] XPath 本身是: /html/body/div[2]/table/tbody/tr/td[2]/p/span/a[2]

driver = webdriver.Chrome(executable_path='C:\chromedriver.exe')
driver.get('')

'''
username_input = '//*[@id="userNameInput"]'
password_input = '//*[@id="passwordInput"]'
submit_button = '//*[@id="submitButton"]'
send_push = '//*[@id="auth_methods"]/fieldset/div[1]/button'
'''

# enters username and password into fields
driver.find_element("xpath", '//*[@id="userNameInput"]').click()
driver.find_element("xpath", '//*[@id="userNameInput"]').send_keys(username)
driver.find_element("xpath", '//*[@id="passwordInput"]').click()
driver.find_element("xpath", '//*[@id="passwordInput"]').send_keys(password)


driver.find_element("xpath", '//*[@id="submitButton"]').click()

# clicks 'send me a push' button on duo mobile screen
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='duo_iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(("xpath", "//button[normalize-space()='Send Me a Push']"))).click()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable(("xpath", '//*[@id="p_p_id_56_INSTANCE_xWhKj4tIFYvm_"]/div/div/div[1]/a[5]'))).click()

# loads next url which has the link on its webpage that needs to be clicked
driver.get('')

# attempts to click on link
driver.find_element("xpath", '/html/body/div[2]/table/tbody/tr/td[2]/p/span/a[2]').click()

I have removed the URLs in driver.get('') as they contain sensitive URLs我已删除driver.get('')中的 URL,因为它们包含敏感 URL

My last line of code is my attempt to click the hyperlink using the XPath我的最后一行代码是我尝试使用 XPath 单击超链接

Any help is appreciated!任何帮助表示赞赏!

sorry I cannot comment due to my reputation.抱歉,由于我的声誉,我无法发表评论。

I assume find_element does not return the correct element hence cannot click.我假设find_element没有返回正确的元素,因此无法点击。 Or you can verify it by changing .click() to .text .或者您可以通过将.click()更改为.text来验证它。 If it returns SITE MAP , then it returns a correct element.如果它返回SITE MAP ,那么它返回一个正确的元素。

I have another solution if you can try.如果您可以尝试,我还有另一个解决方案。 It's using xpath and assumes class='pageheaderlink' is a single element in the HTML body.它使用xpath并假设class='pageheaderlink'是 HTML 主体中的单个元素。

list_subcontent = driver.find_elements_by_xpath(".//span[@class='pageheaderlink']")

for item in list_subcontent:
    if item.text == 'SITE MAP':
        item.click()

help to upvote if this helps如果这有帮助,请帮助投票

Considering the HTML:考虑到 HTML:

元素

The element with text as SITE MAP is a <a> tag and a dynamic element.文本为SITE MAP的元素是<a>标签和动态元素。 To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies :要单击可点击元素,您需要为element_to_be_clickable()诱导WebDriverWait ,您可以使用以下任一定位器策略

  • Using PARTIAL_LINK_TEXT :使用PARTIAL_LINK_TEXT

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "SITE MAP"))).click()
  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.submenulinktext2"))).click()
  • Using XPATH :使用XPATH

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='submenulinktext2' and contains(., 'SITE MAP')]"))).click()
  • Note : You have to add the following imports:注意:您必须添加以下导入:

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

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

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