申科百纳 2020-06-04 10:08 采纳率: 0%
浏览 49
已结题

java建立的Maven项目,利用selenium调用谷歌浏览器访问baidu,为什么页面地址栏显示data;?

  1. 源码如下:

package openBrowser;


import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.openqa.selenium.By;

import java.util.concurrent.TimeUnit;



public class Itest {


public static void main(String[] args) {

//设置chrome驱动位置

System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");

// 初始化一个chrome实例

// ChromeOptions options = new ChromeOptions();

//设置chrome浏览器的参数,使其不弹框提示(chrome正在受自动测试软件的控制)

//options.addArguments("disable-infobars");




WebDriver driver = new ChromeDriver();

//最大化浏览器窗口

driver.manage().window().maximize();

//打开要测试的网址

driver.get("https://baidu.com");

//WebDriver自带智能等待方法

try{

driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

}catch(Exception e){

e.printStackTrace();

}

//获取chrome页面信息

System.out.println("当前打开页面的标题是: " + driver.getTitle());

System.out.println("当前打开页面的URl是:"+driver.getCurrentUrl());


//执行js脚本

String jString ="alert('121')";

((JavascriptExecutor)driver).executeScript(jString);

driver.findElement(By.id("kw")).sendKeys("红星机器");// 输入搜索关键字

//隐式等待

driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);

WebElement element = driver.findElement(By.cssSelector(".red_box"));

((JavascriptExecutor)driver).executeScript("read");


driver.findElement(By.xpath("//a[text()=‘百度搜索’]"));


driver.close();


}

}

jdk版本1.8 selenium版本3.14.0 另外谷歌版本和下载的谷歌驱动版本一致

报错信息:

Starting ChromeDriver 78.0.3904.70 (edb9c9f3de0247fd912a77b7f6cae7447f6d3ad5-refs/branch-heads/3904@{#800}) on port 45179

Only local connections are allowed.

Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.

[1591233930.172][WARNING]: Timed out connecting to Chrome, retrying...

Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created

from no such window: window was already closed

 (Session info: chrome=78.0.3904.70)

Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'

System info: host: 'DESKTOP-KPE10KJ', ip: '172.16.10.22', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_202'

Driver info: driver.version: ChromeDriver

remote stacktrace: Backtrace:

Ordinal0 [0x00AFA903+1550595]

Ordinal0 [0x00A7A701+1025793]

Ordinal0 [0x009FC6E5+509669]

Ordinal0 [0x009F6E24+486948]

Ordinal0 [0x009F7B05+490245]

Ordinal0 [0x009F3A85+473733]

Ordinal0 [0x009FD330+512816]

Ordinal0 [0x009AB649+177737]

Ordinal0 [0x009AA8ED+174317]

Ordinal0 [0x009A8CDB+167131]

Ordinal0 [0x0099144A+70730]

Ordinal0 [0x009924D0+74960]

Ordinal0 [0x00992469+74857]

Ordinal0 [0x00A942C7+1131207]

GetHandleVerifier [0x00B970FD+523789]

GetHandleVerifier [0x00B96E90+523168]

GetHandleVerifier [0x00B9E1E7+552695]

GetHandleVerifier [0x00B978FA+525834]

Ordinal0 [0x00A8B7FC+1095676]

Ordinal0 [0x00A9633B+1139515]

Ordinal0 [0x00A964A3+1139875]

Ordinal0 [0x00A95425+1135653]

BaseThreadInitThunk [0x74F86359+25]

RtlGetAppContainerNamedObjectPath [0x772F7C24+228]

RtlGetAppContainerNamedObjectPath [0x772F7BF4+180]


at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.lang.reflect.Constructor.newInstance(Constructor.java:423)

at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$new$0(W3CHandshakeResponse.java:57)

at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$getResponseFunction$2(W3CHandshakeResponse.java:104)

at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:122)

at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)

at java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)

at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)

at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)

at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)

at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)

at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)

at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)

at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:464)

at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:125)

at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:73)

at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)

at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)

at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)

at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)

at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)

at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)

at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)

at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)

at openBrowser.Itest.main(Itest.java:24)

  • 写回答

3条回答 默认 最新

  • weixin_37841007 2020-06-04 14:30
    关注

    这个我遇到过,但是没找到解决方法。

    你试一下访问本地的一个服务,看看可以不?

    比如,你本地启动一个tomcat,然后加载一个项目,看看直接访问本地服务的url,应该会成功连接。

    直接访问baidu这类服务不在本地的url,会出现这个问题。

    具体我也不知道什么情况。


    你看一下错误的信息,Only local connections are allowed ,所以问题应该在这里。

    你上网查一下有没有这个问题的解决方案。


    找了一个解决方案,你看一下吧。

    https://blog.csdn.net/kang9399052316/article/details/93750929

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?