Python,硒find_element_by_link_text不起作用

时间:2019-02-12 09:20:57

标签: python selenium-webdriver web-scraping selenium-chromedriver

我正在尝试抓取一个网站。我必须在哪里按下链接。为此,我在chrome驱动器中使用了硒库。

from selenium import webdriver

url = 'https://sjobs.brassring.com/TGnewUI/Search/Home/Home?partnerid=25222&siteid=5011&noback=1&fromSM=true#Applications'
browser = webdriver.Chrome()
browser.get(url)
time.sleep(3)

link = browser.find_element_by_link_text("Don't have an account yet?")
link.click()

但是它不起作用。任何想法为什么它不起作用?有解决方法吗?

1 个答案:

答案 0 :(得分:1)

您可以通过多种方式来完成它。这就是其中之一。我使用了driver.execute_script()命令来强制单击。您不应该进行硬编码的延迟,因为它们非常不一致。

修改后的脚本:

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

url = 'https://sjobs.brassring.com/TGnewUI/Search/Home/Home?partnerid=25222&siteid=5011&noback=1&fromSM=true#Applications'

driver = webdriver.Chrome()
driver.get(url)

item = wait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "a[ng-click='newAccntScreen()']")))
driver.execute_script("arguments[0].click();",item)