使用Selenium find_element_by_partial_link_text找不到元素

时间:2016-04-03 21:47:37

标签: python selenium firefox

我试图点击"查看更多结果"以下页面上的按钮:http://www.chadbourne.com/search/people?az[b]=b

我的代码很简单,我尝试过多次迭代。

driver = driver.Firefox()
driver.get("http://www.chadbourne.com/search/people?az[b]=b")
element = driver.find_element_by_partial_link_text("View more results")

element = driver.find_element_by_partial_link_text("view")

element = driver.find_element_by_partial_link_text("results")

无论我尝试哪种上述选项,我都会收到NoSuchElementException。

这很奇怪,因为该元素显然存在于页面上:

<a href="/search/people?s=&amp;az[b]=b&amp;&amp;&amp;&amp;&amp;&amp;&amp;&amp;page=1">View more results</a>

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

页面加载需要一些时间,元素不会立即可用。让我们wait for it 可点击并将定位器更改为CSS选择器:

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


driver = webdriver.Firefox()
driver.get("http://www.chadbourne.com/search/people?az[b]=b")

wait = WebDriverWait(driver, 20)

# get more results
more_results = wait.until(
    EC.element_to_be_clickable((By.CSS_SELECTOR, ".load-more-pager-wrapper .pager-next"))
)
more_results.click()