如何在 python 中使用 selenium web 驱动程序获取文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20996392/
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
How to get text with selenium web driver in python
提问by user3121891
I'm trying to get text using selenium web driver and here is my code. Please note that I don't want to use Xpath, because in my case id gets change on every re-launch of the web page, help please.
我正在尝试使用 selenium web 驱动程序获取文本,这是我的代码。请注意,我不想使用 Xpath,因为在我的情况下,每次重新启动网页时 id 都会更改,请帮忙。
my code:
我的代码:
text=driver.find_element_by_class_name("current-stage").getText("my text")
HTML:
HTML:
<span class="current-text" id="yui_3_7_0_4_1389185744113_384">my text</span>
采纳答案by Arran
You want just .text.
你只想要.text.
You can then verify it afteryou've got it, don't attempt to pass in what you expectit should have.
然后你可以在得到它后验证它,不要试图传递你期望它应该有的东西。
回答by rearThing
Found it, the answer is
找到了,答案是
driver.find_element_by_class_name("ctsymbol").text
回答by kishlaya kumar
You can use:
您可以使用:
element = driver.find_element_by_class_name("class_name").text
This will return the text within the element and will allow you to verify it after that.
这将返回元素内的文本,并允许您在此之后对其进行验证。
回答by subhasis
Thanks this is the correct answer it worked!!
谢谢这是它工作的正确答案!
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 Succesfull")
driver.find_element_by_xpath("//div[@class='pdp-promo-title pdp-title']").click()`enter code here`
SpecialPrice =driver.find_element_by_xpath("//div[@class='pdp-promo-title pdp-title']").text
print(SpecialPrice)
回答by Shubham Jain
Python
Python
element.text
Java
爪哇
element.getText()
C#
C#
element.Text
Ruby
红宝石
element.text

