我试图将webelement内容存储到python列表中。当它工作时,处理约2,000行需要约15分钟。Selenium WebDriver将WebElement数据添加到列表的速度很慢
# Grab webelements via xpath
rowt = driver.find_elements_by_xpath("//tbody[@class='table-body']/tr/th[@class='listing-title']")
rowl = driver.find_elements_by_xpath("//tbody[@class='table-body']/tr/td[@class='listing-location']")
rowli = driver.find_elements_by_xpath("//tbody[@class='table-body']/tr/th/a")
title = []
location = []
link = []
# Add webElement strings to lists
print('Compiling list...')
[title.append(i.text) for i in rowt]
[location.append(i.text) for i in rowl]
[link.append(i.get_attribute('href')) for i in rowli]
有没有更快的方法来做到这一点?
一种方法是发送一些JavaScript浏览器端来从表中提取数据。 Java webdriver绑定提供了一个多功能的'executeJavascript'方法https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/remote/RemoteWebDriver.html#executeScript-java.lang.String-java .lang.Object ...-可以让你做到这一点。下面是一个示例https://medium.com/@danidiaz/extracting-the-contents-of-a-table-in-selenium-ba69b2ca80a9 Python中的相应函数似乎是'execute_script(script,* args)',但它是从文档中不清楚结果是如何反序列化的。 – danidiaz
Python中浏览器的Javascript注入示例:https://johnpauljanecek.github.io/using-javascript-with-python-selenium/ – danidiaz
您可以分享该网站吗?我会有兴趣尝试一些事情。 – JeffC