Python 以及如何从 Selenium 元素 WebElement 对象中获取文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28022764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python and how to get text from Selenium element WebElement object?
提问by Alex
I am trying to get the tag text content in html page by using Selenium methods, but it seems method someElement.getText()
is not available in Python.
Any help, please?
我正在尝试使用 Selenium 方法获取 html 页面中的标签文本内容,但似乎该方法someElement.getText()
在 Python 中不可用。请问有什么帮助吗?
Here's traceback:
这是回溯:
AttributeError: 'WebElement' object has no attribute 'getText'
采纳答案by aberna
Once you locate the element you can use the text method.
找到元素后,您可以使用 text 方法。
Example:
例子:
for element in self.driver.find_elements_by_tag_name('img'):
print element.text
print element.tag_name
print element.parent
print element.location
print element.size
回答by Somesh Samadder
Selenium Get Text From Element(just add ".text")
Selenium 从元素获取文本(只需添加“.text”)
1
1
for all elements of the list
对于列表的所有元素
tree = browser.find_elements_by_xpath(<the_XPATH>)
for i in tree:
print(i.text)
2
2
[ ] fetchby number
[ ] 取数
tree = browser.find_elements_by_xpath(<the_XPATH>)
print(tree[0].text)
回答by Aswin Babu
I was in task to extract text between the target tags but the x.text method didn't work for me. This is because some text are saved as invisible elements .For invisible elements use:
我的任务是在目标标签之间提取文本,但 x.text 方法对我不起作用。这是因为某些文本被保存为不可见元素。对于不可见元素,请使用:
list1 = [x.get_attribute("innertext") for x in driver.find_element_by_xpath(xpath)]
print(list1)