如何在 Python 中使用 Selenium WebDriver 获取文本
- 2025-01-08 08:50:00
- admin 原创
- 107
问题描述:
我正在尝试使用 Selenium WebDriver 获取文本,这是我的代码。请注意,我不想使用 XPath,因为在我的例子中,每次重新启动网页时 ID 都会发生变化。
我的代码:
text = driver.find_element_by_class_name("current-stage").getText("my text")
HTML:
<span class="current-text" id="yui_3_7_0_4_1389185744113_384">my text</span>
我该如何修复此问题?
解决方案 1:
你想要的只是.text
。
获得它之后您就可以验证它,不要尝试传递您期望它应该具有的内容。
解决方案 2:
Python
element.text
Java
element.getText()
代码#
element.Text
红宝石
element.text
解决方案 3:
要打印文本,my text
您可以使用以下任一定位器策略:
使用class_name和
get_attribute("textContent")
:
print(driver.find_element(By.CLASS_NAME, "current-stage").get_attribute("textContent"))
使用css_selector和
get_attribute("innerHTML")
:
print(driver.find_element(By.CSS_SELECTOR, "span.current-stage").get_attribute("innerHTML"))
使用xpath和text属性:
print(driver.find_element(By.XPATH, "//span[@class='current-stage']").text)
理想情况下,您需要诱导WebDriverWait,visibility_of_element_located()
并且可以使用以下任一定位器策略:
使用CLASS_NAME和
get_attribute("textContent")
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "current-stage"))).get_attribute("textContent"))
使用CSS_SELECTOR和文本属性:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.current-stage"))).text)
使用XPATH和
get_attribute("innerHTML")
:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='current-stage']"))).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
您可以在如何使用 Selenium - Python 检索 WebElement 的文本中找到相关讨论
参考
有用文档的链接:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性返回The text of the element.
使用 Selenium 时文本和 innerHTML 之间的区别
解决方案 4:
答案是:
driver.find_element_by_class_name("ctsymbol").text
解决方案 5:
您可以使用:
element = driver.find_element_by_class_name("class_name").text
这将返回元素内的文本并允许您随后验证它。
解决方案 6:
这是正确答案。成功了!!
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
driver = webdriver.Chrome("E:\\Python\\selenium\\webdriver\\chromedriver.exe")
driver.get("https://www.tatacliq.com/global-desi-navy-embroidered-kurta/p-mp000000000876745")
driver.set_page_load_timeout(45)
driver.maximize_window()
driver.implicitly_wait(2)
driver.get_screenshot_as_file("E:\\Python\\Tatacliq.png")
print ("Executed Successfully")
driver.find_element_by_xpath("//div[@class='pdp-promo-title pdp-title']").click()
SpecialPrice = driver.find_element_by_xpath("//div[@class='pdp-promo-title pdp-title']").text
print(SpecialPrice)
解决方案 7:
在 Selenium 4 更新后,任何发现此主题的人都应注意。driver.find_element_by_* 已被弃用,使用它将给出“弃用警告”。替换方法是:driver.find_element(By.X,"name")请查找 Selenium 4 信息。
解决方案 8:
当无法在自定义类中获取某些内容或者更改 id 时,我发现这绝对是无价的:
driver.find_element_by_xpath("//*[contains(text(), 'Show Next Date Available')]").click()
driver.find_element_by_xpath("//*[contains(text(), 'Show Next Date Available')]").text
driver.find_element_by_xpath("//*[contains(text(), 'Available')]").text
driver.find_element_by_xpath("//*[contains(text(), 'Avail')]").text
解决方案 9:
从元素中获取文本:
url=driv.find_element(By.whatDoYouWant, "ClassNameOrwhatDoYouWant").text
print(url)
前任:
url = web.find_element(By.TAG_NAME, "a").text
print(url)
从元素中获取文本:
前任:
for i in range(6):
var = web.find_elements(By.TAG_NAME, "input")[i].text
print(var)
扫码咨询,免费领取项目管理大礼包!