find_elements_by_partial_link_text将找不到元素

时间:2016-06-05 18:48:38

标签: python selenium selenium-chromedriver

在我访问的网站中,有以下结构的7个链接:

<tr>
 <td>
  <a href="some link i will need to visit" title="some title"> some text... Episode ....
  </a>
 </td>
 <td> some date </td>
</tr>

现在我使用以下代码来获取剧集并将它们放入列表中

chromedriver = "C:/.../chromedriver.exe"

driver = webdriver.Chrome(chromedriver)

driver.get("link containing the content")

episodes = driver.find_elements_by_partial_link_text('Episode')

print "episodes found: ", len(episodes)

这始终会打印episodes found: 0。我试过从超链接文本的开头使用一个片段,但它仍然无法正常工作。任何帮助将不胜感激。

The link is this

2 个答案:

答案 0 :(得分:2)

除了@nullpointer null指出之外,请注意网页加载的延迟 - 您要查找的元素无法立即显示,您需要wait for them to be present

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

driver = webdriver.Chrome()
driver.get("https://kissanime.to/Anime/Hunter-x-Hunter-2011-Dub")

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//a[contains(@title,'Episode')]")))

episodes = driver.find_elements_by_xpath("//a[contains(@title,'Episode')]")
print(len(episodes))

driver.close()

打印8

答案 1 :(得分:1)

查看提供DOM的链接

<a href="/Anime/Hunter-x-Hunter-2011-Dub/Episode-007?id=126436" title="Watch anime Hunter x Hunter (2011) (Dub) Episode 007 online in high quality"> Hunter x Hunter (2011) (Dub) Episode 007</a>

在这种情况下,您也可以使用以下内容:

episodes = driver.find_elements_by_xpath("//a[contains(@title,'Episode')]")

编辑 :如果您想引用chromedriver的正确用法。看看:

Running webdriver chrome with Selenium