Selenium 如何获取标题文本?
Selenium How To Get Title Text?
<span class="Voting__button Voting__button-up">
<a href="#" title="Upvote"> ---> Need get title word in my textbox
我这样试过:
textbox1.text= driver.findelement(by.xpath("//*[@id='posts_list']/ul/li[1]/div/div[2]/div[2]/div[2]/span[1]/span/span/a")).Text;
我想获得这个称号,但它不起作用。
您可以使用Selenium
getAttribute
方法获取标题词。
尝试使用下面的 XPATh
:
Java代码:
textbox1.text = driver.findelement(By.xpath("//*[@id='posts_list']//*[contains(@class,'Voting__button-up')]/a")).getAttribute("title");
C#代码:
textbox1.text = driver.FindElement(By.XPath("//*[@id='posts_list']//*[contains(@class,'Voting__button-up')]/a")).GetAttribute("title");
正如您提供的 C#
标签,method()
不是 findelement()
而是 FindElement()
.
因此,根据您提供的 HTML
提取 title
值 Upvote
您可以使用以下代码行:
textbox1 = driver.FindElement(By.XPath("//*[@id='posts_list']//span[@class='Voting__button Voting__button-up']/a")).GetAttribute("title");
<span class="Voting__button Voting__button-up">
<a href="#" title="Upvote"> ---> Need get title word in my textbox
我这样试过:
textbox1.text= driver.findelement(by.xpath("//*[@id='posts_list']/ul/li[1]/div/div[2]/div[2]/div[2]/span[1]/span/span/a")).Text;
我想获得这个称号,但它不起作用。
您可以使用Selenium
getAttribute
方法获取标题词。
尝试使用下面的 XPATh
:
Java代码:
textbox1.text = driver.findelement(By.xpath("//*[@id='posts_list']//*[contains(@class,'Voting__button-up')]/a")).getAttribute("title");
C#代码:
textbox1.text = driver.FindElement(By.XPath("//*[@id='posts_list']//*[contains(@class,'Voting__button-up')]/a")).GetAttribute("title");
正如您提供的 C#
标签,method()
不是 findelement()
而是 FindElement()
.
因此,根据您提供的 HTML
提取 title
值 Upvote
您可以使用以下代码行:
textbox1 = driver.FindElement(By.XPath("//*[@id='posts_list']//span[@class='Voting__button Voting__button-up']/a")).GetAttribute("title");