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

时间:2020-08-14 22:53:39

标签: python python-3.x selenium

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()

以下是网站上的信息,我想单击“销售”,但似乎不能这样做,希望对您有所帮助

a _ngcontent-hyf-c12 =“” routerlink =“ ./ SalesReport” routerlinkactive =“活动” href =“ / Reports / SalesReport”销售/ a

Pic of error

如果我尝试使用XPATH单击它,则会显示此内容 Error Pic

1 个答案:

答案 0 :(得分:0)

在您的html图片中,售后出现空白。
仔细查看:href="/Reports/SalesReport">Sales </a

因此find_element_by_link_text('Sales')将不起作用。
您可以将其更改为find_element_by_link_text('Sales ')

但是,这样会更好:

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