【问题标题】:How to retrieve the HTML of an element through SeleniumWebdriver?如何通过 Selenium Webdriver 检索元素的 HTML?
【发布时间】:2019-02-15 10:46:57
【问题描述】:

我正在尝试在 Mac 上向 Selenium Webdriver 发送短信。我试图让它自动填充谷歌和搜索的搜索空白。搜索框的html如下:

<input class="gLFyf" maxlength="2048" name="q" type="text" jsaction="paste:puy29d" aria-autocomplete="both" aria-haspopup="false" autocapitalize="off" autocomplete="off" autocorrect="off" role="combobox" spellcheck="false" title="Search" value="" aria-label="Search">

所以,我想测试一下我是否可以挑出上面的元素,它对应于搜索框。所以我让它打印以下内容,看看它是否确实得到了元素:

driver.findElements(By.className("gLFyf")).toString

但是,它没有打印上面的实际 html,而是打印了

[[[ChromeDriver: chrome on MAC (a8470f41df7943e813ac6f77266ed33c)] -> class name: gLFyf]]

谁能向我解释为什么我没有得到元素?

【问题讨论】:

  • 如果你刷新页面,类名和你发布的一样吗?我不认为它是。你可以试试By.cssSelector("input[name='q']")。您将遇到的另一个问题是.findElements() 返回一个元素集合。你不能在一个集合上做.toString(),你必须在一个元素上做,但它不会做你所期望的。如果你想要元素的 HTML,那么你想要driver.findElement(By.cssSelector("input[name='q']")).getAttribute("outerHTML")。注意使用.findElement()(单数)而不是.findElements()(复数)
  • toString() 方法不会打印 webElement 的 HTML; selenium 中的 webelement 对象与 DOM 中的节点不同。此外,您使用的是findElements 而不是findElement,因此您实际上只有一个网络元素列表,可能只包含一项。

标签: java selenium selenium-webdriver webdriver outerhtml


【解决方案1】:

根据您要打印 Google 主页搜索框 元素的 html 的问题,您可以使用以下解决方案:

  • 代码块:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class findElement_html {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.get("https://www.google.com/");
            WebElement myElement = driver.findElement(By.name("q"));
            System.out.println(myElement.getAttribute("outerHTML"));
        }
    }
    
  • 控制台输出:

    <input class="gsfi lst-d-f" id="lst-ib" maxlength="2048" name="q" autocomplete="off" title="Search" value="" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: transparent url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) repeat scroll 0% 0%; position: absolute; z-index: 6; left: 0px; outline: currentcolor none medium;" dir="ltr" spellcheck="false" type="text">
    

【讨论】:

    【解决方案2】:

    我不明白你想做什么? 你想检查你是否找到了你需要的元素?

    所以..

    Webelement element = driver.findElement...
    

    如果没有找到元素会抛出异常。

    您可以尝试检查定位器找到的元素数量:

    driver.findElements(By.className("gLFyf")).count
    

    如果没有找到,这将返回 0,您也可以将其与 1 进行比较,以确保没有任何其他具有相同类名的元素..

    if(driver.findElements(By.className("gLFyf")).count > 1){
         //more than one lement found
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-09
      • 2013-07-18
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多