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
如果我尝试使用XPATH单击它,则会显示此内容 Error Pic
答案 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')]")