
# -*- coding: utf-8 -*-
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
PATH='C:\\Users\\admin\\Documents\\chromedriver.exe'
driver = webdriver.Chrome(PATH,chrome_options=options)
driver.get('https://www.futunn.com/en/stock/FUTU-US')
freeflowtickers=driver.find_element_by_xpath("//*[@id=\"app\"]/div/div[3]/section[1]/div[4]/div[9]/div[4]/div[2]")
print(freeflowtickers)
print(freeflowtickers.text)
然后運行它,在控制臺中,你得到
<selenium.webdriver.remote.webelement.WebElement (session="08bc2133e5e3b6911cea32c6750833a8", element="e38cb418-2e37-428d-af4b-e01dd3e691ae")>
但在它之后沒有文本被獲取。
這怎么會發生?
uj5u.com熱心網友回復:
所以這不是最干凈的解決方案,但我只是測驗了它并且它正在作業。
float_element = driver.find_element_by_xpath('//*[@id="app"]/div/div[3]/section[1]/div[4]/div[9]/div[4]')
number = float_element.text.split("\n")[1]
我已經獲取了父 div 標簽的 xpath,其中包含兩行文本“Float”和由換行符分隔的數字。
uj5u.com熱心網友回復:
要列印文本64.11M,您可以使用以下任一定位器策略:
使用xpath和
get_attribute("innerHTML"):print(driver.find_element(By.XPATH, "//div[text()='Float']//following-sibling::div[1]").get_attribute("innerHTML"))使用xpath和text屬性:
print(driver.find_element(By.XPATH, "//div[text()='Float']//following-sibling::div[1]").text)
理想情況下,您需要為visibility_of_element_located()引入WebDriverWait,您可以使用以下任一定位器策略:
使用XPATH和text屬性:
driver.get("https://www.futunn.com/en/stock/FUTU-US") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='stock-detail-btn']/i"))).click() print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Float']//following-sibling::div[1]"))).text)使用XPATH和
get_attribute("innerHTML"):driver.get("https://www.futunn.com/en/stock/FUTU-US") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='stock-detail-btn']/i"))).click() print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[text()='Float']//following-sibling::div[1]"))).get_attribute("innerHTML"))注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
您可以在How toret the text of a WebElement using Selenium - Python 中找到相關討論
參考
鏈接到有用的檔案:
get_attribute()方法Gets the given attribute or property of the element.text屬性回傳The text of the element.- 使用 Selenium 的 text 和 innerHTML 之間的區別
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/381145.html
標籤:Python 硒 硒网络驱动程序 网络驱动程序等待 获取属性
上一篇:在手機上渲染PDF
