在Java中如何使用Selenium选择此元素?
我试图在ChromeDriver上选择JavaSelenium中的值1352在Java中如何使用Selenium选择此元素?,java,selenium,selenium-chromedriver,selector,classname,Java,Selenium,Selenium Chromedriver,Selector,Classname,我试图在ChromeDriver上选择JavaSelenium中的值1352 <span class="numfound" id="yui_3_18_1_1_1522936314968_15">1352</span> 也许我的CSS选择器错了?使用numfound选择此元素最直观的方法是什么 已解决:我很傻,没有按照我的要求使用.getText()。只能对标记内部的元素使用By选择器 获取元素的文本的步骤 你可以用 driver.findElement(By.xpath
<span class="numfound" id="yui_3_18_1_1_1522936314968_15">1352</span>
也许我的CSS选择器错了?使用numfound选择此元素最直观的方法是什么
已解决:我很傻,没有按照我的要求使用.getText()。只能对标记内部的元素使用By选择器 获取元素的文本的步骤
driver.findElement(By.xpath(“//span[@class='numfound']”)代码>
或者(如果您更喜欢):
driver.findElement(By.className(“numfound”).getText()代码>
String source=driver.getPageSource()代码>
WebElement element = browser.findElement(By.className("numfound"));
//do whatever you want with your element, get attributes, etc.
仅供参考:此跨度是一个WebElement。您可以使用WebElement执行某些操作。其中一些是:
1. click on it. (Provided that element must be clickable)
2. getText() : Text between the <span> and </span> tag.
3. getSize();
4. getLocation();
5. getScreenShotAs(OUTPUT.Type)
6. getRect();
7. SendKeys(charSequence) (Provided that it can take input something).
并对其执行字符串操作。如果您对此有任何疑问,请告诉我。您能显示实际代码吗?啊,我忘了使用.getText()。非常感谢你!
WebElement element = browser.findElement(By.className("numfound"));
//do whatever you want with your element, get attributes, etc.
1. click on it. (Provided that element must be clickable)
2. getText() : Text between the <span> and </span> tag.
3. getSize();
4. getLocation();
5. getScreenShotAs(OUTPUT.Type)
6. getRect();
7. SendKeys(charSequence) (Provided that it can take input something).
String spanText = driver.findElement(by.cssSelector("span[class="numfound"]")).getText();