如何使用 selenium xpath 复制文本?当我写
driver.find_elements_by_xpath("//div[@class='rankingItem-value js-countable']").text
我收到下一个错误:
Traceback (most recent call last):
File "<stdin>", line 15, in <module>
AttributeError: 'list' object has no attribute 'text'
完整代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get('https://www.similarweb.com/')
driver.find_element_by_id("js-swSearch-input").send_keys("www.pornhub.com")
driver.find_element_by_css_selector("button.swSearch-submit").click()
#self.driver.implicitly_wait(30)
time.sleep(10)
content = driver.find_elements_by_xpath("//div[@class='rankingItem-value js-countable']").text
print(content)
我需要将站点的全球排名复制到一个表中,“22”一个。如何?
原文由 Nia Grace 发布,翻译遵循 CC BY-SA 4.0 许可协议
要选择所需的元素,您需要使用
find_element_by_xpath(xpath)
方法(这将返回与xpath
匹配的第一个网络元素)或find_elements_by_xpath(xpath)[0]
匹配的第一个网络元素(该网络元素将首先返回–来自xpath
匹配的元素列表)。另外你的XPath
是不正确的,因为没有div
和class="rankingItem-value js-countable"
你需要span
-ff.eada-5.所以尝试以下:或者
如果您需要获得“国家排名”或“类别排名”,请使用以下内容: