![](/img/trans.png)
[英]python/Selenium --> find_elements_by_xpath method not finding all elements
[英]Selenium Python Find Elements by XPath does not return all expected elements
我正在从 HTML 网站上的一系列表格中抓取数据。 该网站根据输入有不同数量的表格,因此我尝试使用: .find_elements_by_xpath("//table")
来简单地获取包含页面上所有表格元素的列表。
但是,在这种情况下,它只返回页面上的第一个表。 当我使用find_element_by_xpath(//table[2])
,它返回另一个表,但使用find_elements
不会。
网站门户在这里,只需按底部的发送。 (我正在尝试从结果页面获取数据)。 我在 Firefox 浏览器上使用 Python 中的 Selenium。
有趣的是,头表有一个table[1]
的 XPath,这就引出了table[0]
在哪里的问题。
这里的关键是在导航到新选项卡之前正确等待。 在点击发送后,一个新标签被打开。 见下面的代码:
driver.get('http://imed.med.ucm.es/Tools/rankpep.html')
WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//img[@src='/icons/ig_logo.png']")))
send = driver.find_element_by_xpath("//input[@value='Send']")
driver.execute_script("arguments[0].scrollIntoView();", send)
driver.execute_script("arguments[0].click();", send)
WebDriverWait(driver, 20).until(EC.number_of_windows_to_be(2)) #Wait for 2nd tab to open
driver.switch_to.window(driver.window_handles[1])
count = driver.find_elements_by_xpath("//table")
print(len(count))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.