如何用硒xpath複製文本? 當我寫python硒如何複製文本?
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」 之一。怎麼樣?
'find_elements_by_xpath'返回列表元素。您必須迭代元素並從每個元素中檢索文本。 –
可能的重複[如何使用Selenium Python xpath選擇元素](http://stackoverflow.com/questions/19035186/how-to-select-element-with-selenium-python-xpath) – parik