Selenium click() 方法在 Angular.js 网站上



我正在抓取一个棱角分明.js的网站。我的初始链接有一个搜索按钮。我通过 xpath 找到并单击没有问题。单击搜索后,我希望能够单击列表中的每个运动员以转到他们的信息页面,但我使用单击方法没有成功。这些链接附在其名称上。

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
TIMEOUT = 5
driver = webdriver.Firefox()
driver.set_page_load_timeout(TIMEOUT)
url = 'https://n.rivals.com/search#?formValues=%7B%22sport%22:%22Football%22,%22recruit_year%22:2021,%22offer_and_visit_type%22:%5B%22Offer%22%5D,%22prospect_profiles.prospect_colleges.offer%22:true,%22page_number%22:1,%22page_size%22:50%7D'
try:
driver.get(url)
except TimeoutException:
pass
search_button = driver.find_element_by_xpath('//*[@id="articles"]/div/div[2]/div/div/div[1]/form/div[2]/div[5]/button')
search_button.click();
#below is where I tried, but could not get to click
first_athlete = driver.find_element_by_xpath('//*[@id="content_"]/td[1]/div[2]/a')
first_athlete.click();

如果删除 xpath 中的最后一个/a,则有效:

first_athlete = driver.find_element_by_xpath('//*[@id="content_"]/td[1]/div[2]')
first_athlete.click()

如果要搜索所有运动员并且随身携带运动员姓名,也可以使用CSS选择器。

athelete = driver.find_elements_by_css_selector(`#content_ > td > div > a[href *="donovan-jackson"]);
athelete.click();

此代码将为每个玩家提供一个唯一的 Web 元素。

谢谢

最新更新