如何在python中使用selenium web驱动程序获取文本

时间:2014-01-08 13:00:35

标签: python selenium

我正在尝试使用selenium web驱动程序获取文本,这是我的代码。请注意,我不想使用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>

7 个答案:

答案 0 :(得分:91)

您只需要.text

然后你可以在之后验证它,不要试图传递你应该拥有的期望

答案 1 :(得分:40)

<强>的Python

element.text

Java

element.getText()

C#

element.Text

<强>红宝石

element.text

示例(在Python中):

element=driver.find_element_by_class_name("nameOfClass")
print(element.text)

OR

driver.find_element_by_class_name("nameOfClass").text

示例(在Java中):

WebElement element= driver.findElement(By.id(WebelementID));
System.out.println("Printing " + element.getText());

OR

driver.findElement(By.id(WebelementID)).getText());

答案 2 :(得分:5)

找到它,答案是

driver.find_element_by_class_name("ctsymbol").text

答案 3 :(得分:4)

你可以做到,

element = driver.find_element_by_class_name("class_name").text

这将返回文本并在此之后进行验证。

答案 4 :(得分:3)

谢谢,这是正确的答案!!

#!/usr/bin/env python

from Tkinter import *
from PIL import Image, ImageTk

"""Asks for picture file. Loads picture file with '.jpg' appended to name entered.
Double Click and F11 both resume fullscreen.
Triple Click and Escape both close fullscreen."""

def choose_picture():
    user_input = raw_input("Choose picture file: ")
    user_input = (user_input + ".jpg")

    root = Tk()
    root.state = True
    root.attributes("-fullscreen", True)

    main_image = Image.open(user_input)  # Room label image
    photo = ImageTk.PhotoImage(main_image)
    Label(root, image=photo).pack()

    def resume_fullscreen(self, event=None):
        root.state = True
        root.attributes("-fullscreen", True)
        return "break"

    def end_fullscreen(self, event=None):
        root.state = False
        root.attributes("-fullscreen", False)
        root.geometry("200x200")
        return "break"

    root.bind("<Double-1>", resume_fullscreen)
    root.bind("<Triple-1>", end_fullscreen)
    root.bind("<F11>", resume_fullscreen)
    root.bind("<Escape>", end_fullscreen)
    root.mainloop()

choose_picture()

答案 5 :(得分:2)

要打印文本my text,您可以使用以下任一Locator Strategies

  • 使用 class_nameget_attribute("textContent")

    print(driver.find_element(By.CLASS_NAME, "current-stage").get_attribute("textContent"))
    
  • 使用 css_selectorget_attribute("innerHTML")

    print(driver.find_element(By.CSS_SELECTOR, "span.current-stage").get_attribute("innerHTML"))
    
  • 使用 xpathtext 属性:

    print(driver.find_element(By.XPATH, "//span[@class='current-stage']").text)
    

理想情况下,您需要为 visibility_of_element_located() 引入 WebDriverWait,并且您可以使用以下任一 Locator Strategies

  • 使用 CLASS_NAMEget_attribute("textContent")

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "current-stage"))).get_attribute("textContent"))
    
  • 使用 CSS_SELECTORtext 属性:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.current-stage"))).text)
    
  • 使用 XPATHget_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
    
<块引用>

您可以在 How to retrieve the text of a WebElement using Selenium - Python

中找到相关讨论

参考文献

链接到有用的文档:

答案 6 :(得分:0)

当无法在自定义类中获取某些内容或更改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