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
我该如何点击“点击”按钮;“复制”;ref与Selenium ChromeDriver Java实现_Java_Selenium_Selenium Webdriver_Selenium Chromedriver - Fatal编程技术网

我该如何点击“点击”按钮;“复制”;ref与Selenium ChromeDriver Java实现

我该如何点击“点击”按钮;“复制”;ref与Selenium ChromeDriver Java实现,java,selenium,selenium-webdriver,selenium-chromedriver,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,如何单击此URL中的“复制”按钮 我需要单击的标记被标记为“复制” 请看所附图片 请看下面我的代码 String url = "https://www.w3resource.com/java-exercises/basic/java-basic-exercise-249.php"; System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver"); ChromeOptions op

如何单击此URL中的“复制”按钮

我需要单击的标记被标记为“复制”

请看所附图片

请看下面我的代码

    String url = "https://www.w3resource.com/java-exercises/basic/java-basic-exercise-249.php";
    System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
    ChromeOptions options = new ChromeOptions();
    options.setPageLoadStrategy(PageLoadStrategy.NONE);
    options.addArguments("--no-sandbox");
    //options.addArguments("--headless");
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions");

    ChromeDriver driver = new ChromeDriver(options);
    driver.get(url);
    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
    WebDriverWait wait = new WebDriverWait(driver,30);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[text()='Copy']")));
错误日志的内容如下:

    INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //a[text()='Copy'] (tried for 30 second(s) with 500 milliseconds interval)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'studentmacbookpro.local', ip: '2406:e003:8ae:4201:9caf:932f:bb9:64e4%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.15.3', java.version: '9.0.4'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion:     81.0.4044.138, chrome: {chromedriverVersion: 81.0.4044.138 (8c6c7ba89cc9..., userDataDir: /var/folders/j0/ktvnz6n91kg...}, goog:chromeOptions: {debuggerAddress: localhost:49653},  javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: none, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:virtualAuthenticators: true}
Session ID: 127865859a666d5c2e508415f9d94b3f
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at src.ChromeDriverMac.main(ChromeDriverMac.java:33)

此按钮仅在用鼠标悬停代码段时显示。在您的示例中,您只需打开页面

请参见此问题以了解如何将鼠标悬停在元素上:

之后,您将看到显示“复制”按钮

UPD:

另一个可能导致您出现问题的因素是,通过.xpath(“//a[text()='Copy']”查找的
元素不是您希望看到的元素。基本上,您正在等待与DOM中指定xpath匹配的元素的可见性


页面上有几个片段,每个片段都有“复制”按钮。您可能希望第二个代码段中的按钮可见,但是您的服务员正在等待第一个代码段中的按钮。

此按钮仅在您用鼠标悬停代码段时显示。在您的示例中,您只需打开页面

请参见此问题以了解如何将鼠标悬停在元素上:

之后,您将看到显示“复制”按钮

UPD:

另一个可能导致您出现问题的因素是,通过.xpath(“//a[text()='Copy']”
查找的
元素不是您希望看到的元素。基本上,您正在等待与DOM中指定xpath匹配的元素的可见性


页面上有几个片段,每个片段都有“复制”按钮。您可能希望第二个代码段中的按钮可见,但是您的服务员正在等待第一个代码段中的按钮。

只有当我们悬停代码段时,“复制”按钮才会启用

这可以通过使用以下代码实现-

    Actions action = new Actions(driver);
    action.moveToElement(driver.findElement(By.cssSelector("div.code-toolbar"))).moveToElement(driver.findElement(By.xpath("//a[text() = 'Copy']"))).click().build().perform();
这里我导入Actions类来实现鼠标操作。导入之后,我告诉我的动作类先移动到代码片段部分,然后移动到复制按钮,然后单击该按钮


有关更多详细信息,您可以执行鼠标和键盘操作。

只有在代码段悬停时,“复制”按钮才会启用

这可以通过使用以下代码实现-

    Actions action = new Actions(driver);
    action.moveToElement(driver.findElement(By.cssSelector("div.code-toolbar"))).moveToElement(driver.findElement(By.xpath("//a[text() = 'Copy']"))).click().build().perform();
这里我导入Actions类来实现鼠标操作。导入之后,我告诉我的动作类先移动到代码片段部分,然后移动到复制按钮,然后单击该按钮

有关更多详细信息,您可以通过鼠标和键盘操作