当前位置: 首页 > 知识库问答 >
问题:

Selenium Java代码打印google搜索的前五个结果

仲孙向明
2023-03-14

线程“main”org.openqa.selenium.StaleElementReferenceException:过时元素引用:元素未附加到页面文档(会话信息:chrome=83.0.4103.106)有关此错误的文档,请访问:https://www.seleniumhq.org/exceptions/stale_element_reference.html构建信息:版本:“3.141.59”,修订:“E82BE7D358”,时间:“2018-11-14T08:25:53”驱动程序信息:org.openqa.selenium.chromeDriver Capabilities{AcceptinSecrecertsUTHN:VirtualAuthenticators:true}会话ID:f9fd73fa86d306099bad2835337c25c0***元素信息:{using=XPath,value=//H3[@class='LC20LB DKV0MD']}

代码快照

显示第一个页面的结果,但当我单击第二个页面结果时,我得到StaleElementReferenceException

import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class NavigateToGoogle {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.google.com");
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        //driver.findElement(By.xpath("//input[@class='gLFyf gsfi'])")).sendKeys("zahid");

        WebElement element=driver.findElement(By.name("q"));
        element.sendKeys("mohammed zahid");
        //element.click();
        Thread.sleep(3000);
        //driver.findElement(By.xpath("(//input[@type='submit'])[3]")).click();
        List<WebElement> button=driver.findElements(By.name("btnK"));
        System.out.println(button.size());
        WebElement submit=driver.findElement(By.name("btnK"));
        submit.submit();

        WebElement matchingRes=driver.findElement(By.xpath("//h3[@class='LC20lb DKV0Md']"));
        List<WebElement> listRes=matchingRes.findElements(By.xpath("//h3[@class='LC20lb DKV0Md']"));
        System.out.println(listRes.size());
        for(WebElement results:listRes) 
        {
            String value = results.getText();
            System.out.println(value);
        }

        driver.findElement(By.xpath("//a[@aria-label='Page 2']")).click();
        WebDriverWait wait = new WebDriverWait(driver, 5);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h3[@class='LC20lb DKV0Md']")));
        WebElement matchingRes1=driver.findElement(By.xpath("//h3[@class='LC20lb DKV0Md']")); 
        matchingRes1.getSize();
        List<WebElement> listRes1=matchingRes.findElements(By.xpath("//h3[@class='LC20lb DKV0Md']")); 
        System.out.println(listRes1.size());

        for(WebElement results1:listRes1)
        {
            String value = results1.getText();
            System.out.println(value);
        }
    }

}

共有1个答案

郜卓君
2023-03-14

我们不能硬编码谷歌结果页面元素,因为每个谷歌页面每天都在更新。这里我给出了结果搜索的示例代码,您可以尝试这样做(我打开谷歌页面,键入“测试”并显示所有结果,然后单击列表中的“测试类型”,如果您需要所有结果,您可以打印)

 public static void main(String[] args) throws Exception {
    System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\Chromedriver\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("https://www.google.com/");
    driver.findElement(By.name("q")).sendKeys("Testing");
    Thread.sleep(Long.parseLong("1000"));
    List<WebElement> LIST = driver.findElements(By.xpath("//ul[@role='listbox']//li/descendant::div[@class='sbl1']"));
    System.out.println(LIST.size());


    for (int i = 0; i < LIST.size(); i++)
    {
        //System.out.println(LIST.get(i).getText());
        if (LIST.get(i).getText().contains("testing types"))
        {
            LIST.get(i).click();
            break;
        }
    }
}

}

 类似资料:
  • 本文向大家介绍vue2实现搜索结果中的搜索关键字高亮的代码,包括了vue2实现搜索结果中的搜索关键字高亮的代码的使用技巧和注意事项,需要的朋友参考一下 具体代码如下所示: 开源项目地址: github.com/alex-0407/v… 效果演示 总结 以上所述是小编给大家介绍的vue2实现搜索结果中的搜索关键字高亮的代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此

  • 问题内容: 任何人都可以分享一些Java代码来开始使用Google搜索API。可以帮助我。(我已获得API密钥和自定义搜索引擎ID)。 谢谢。 问题答案: 我已经更改了@Zakaria提供的代码中的。这可能不是解决问题的正确方法,但是它为您提供了Google搜索的结果链接。您只需要解析输出。看这里, 希望它也对您有用。

  • 我有一个用于搜索的RESTendpoint。

  • 问题内容: 我必须打印Java项目的整个源代码。最终版本应如下所示: Eclipse:File- > Print。但是使用此功能,您一次只能打印一个文件。 有没有一种方法可以使用一个命令打印(或创建pdf / rtf)整个项目(所有 .java,。xml,…文件)? 我在Windows XP SP3上使用Eclipse Galileo 编辑: 对于每个类/文件,页面应(或多或少)如下所示: C:\

  • $ gdb -q `which gdb` (gdb) l 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18

  • 例子 $ gcc -v foo.c ... ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu" ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../../x86_64-linux-gnu/include" #incl