Selenium webdriver 如何在selenium中获取div内的标记文本
我想要h1的文本,但如何得到它,我不知道,我在硒新。回答我。提前谢谢。请忽略h1和div开始标记处的“\”Selenium webdriver 如何在selenium中获取div内的标记文本,selenium-webdriver,Selenium Webdriver,我想要h1的文本,但如何得到它,我不知道,我在硒新。回答我。提前谢谢。请忽略h1和div开始标记处的“\” <\div class="on minimum coverage VPM2" title="This quote includes the minimum coverage levels required by your state."> <\h1>$74.80/month<\/h1>
<\div class="on minimum coverage VPM2" title="This quote includes the minimum coverage levels required by your state.">
<\h1>$74.80/month<\/h1>
<\/div>
假设您希望获得h1标记的innerHTML/文本,即java代码下面的$74.80/月将起作用:
String text = driver.findElement(By.xpath("//div[contains(@class,'on minimum coverage VPM2')]/h1")).getText();
上面的答案几乎是正确的,但是当我遇到几乎完全相同的问题时,使用getText只会返回一个空字符串。不管我是尝试xpath还是CSS来定位元素,它都没有任何区别。事实上,这个位置是一个转移注意力的问题——它正在寻找元素,只是h标记没有返回文本 试试这个
String text = driver.findElement(By.xpath("//*[@class="on minimum coverage VPM2']/h1")).getAttribute("innerHTML);
以下是简单的解决方案:
String titleElem = driver.findElement(By.xpath("//*[@class='on minimum coverage VPM2']/h1")).getAttribute("innerHTML”);
assertEquals("Working as charm", titleElem);
这甚至不是有效的标记。您的Selenium代码在哪里?请使用:driver.findElementsBy.tagnameh1但在我的HTML页面中,这不仅仅是一个h1标记,还有许多h1标记,因此如何获得这个特定的h1标记。谢谢