Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中如何使用Selenium选择此元素?_Java_Selenium_Selenium Chromedriver_Selector_Classname - Fatal编程技术网

在Java中如何使用Selenium选择此元素?

在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

我试图在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']”)

    或者(如果您更喜欢):

    driver.findElement(By.className(“numfound”).getText()

  • 或者通过以下方式从页面源获取它:

    String source=driver.getPageSource()

  • 并从中提取一个字符串,以“numfound”开头,以下面的标记结尾 然后从此行中提取字符串。

    您只需执行以下操作:

    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();