gpt4 book ai didi

java - 如何通过 Selenium Webdriver 检索元素的 HTML?

转载 作者:行者123 更新时间:2023-12-02 10:49:43 26 4
gpt4 key购买 nike

我正在尝试在 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]]

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

最佳答案

根据您的问题,要打印 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">

关于java - 如何通过 Selenium Webdriver 检索元素的 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52267296/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com