简体   繁体   English

找不到使用 selenium(Python 3.7)单击 href 链接的方法

[英]Can't find a way to click on an href link with selenium (Python 3.7)

Pic of inspect element检查元素的图片

from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get('https://website.com/')
driver.maximize_window()
search = driver.find_element_by_id('UserName')
search.send_keys('UserName')
search = driver.find_element_by_id('Password')
search.send_keys('Password')
search.send_keys(Keys.RETURN)
try:
    element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.LINK_TEXT, "Admin"))
    )
    element.click
    link = driver.find_element_by_link_text('Admin')
    link.click()
    
    element = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.LINK_TEXT, "Reports"))
    )
    element.click
    link = driver.find_element_by_link_text('Reports')
    link.click()
    
except:
    driver.quit()
    
driver.implicitly_wait(5)
sales_link = driver.find_element_by_link_text('Sales').click()

Below is the info from the website, I want to click on Sales but can't seem to do so any help would be appreciated以下是来自网站的信息,我想点击销售,但似乎无法这样做,任何帮助将不胜感激

a _ngcontent-hyf-c12="" routerlink="./SalesReport" routerlinkactive="active" href="/Reports/SalesReport"Sales /a a _ngcontent-hyf-c12="" routerlink="./SalesReport" routerlinkactive="active" href="/Reports/SalesReport"Sales /a

Pic of error错误图片

This what appears if I try to click on it with XPATH Error Pic如果我尝试使用 XPATH错误图片单击它会出现什么

In your html pic, I see a whitespace after Sales.在您的 html 图片中,我在售后看到了一个空格。
Look carefully: href="/Reports/SalesReport">Sales </a .仔细看: href="/Reports/SalesReport">Sales </a

So find_element_by_link_text('Sales') will not work.所以find_element_by_link_text('Sales')将不起作用。
You can change it to find_element_by_link_text('Sales ') .您可以将其更改为find_element_by_link_text('Sales ')

However, this will be better:但是,这会更好:

driver.find_elements_by_xpath("//a[contains(text(), 'Sales')]")

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

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