1

How do I click the "Copy" button from this URL https://www.w3resource.com/java-exercises/basic/java-basic-exercise-249.php

enter image description here

The tag I need to click is tagged as "Copy"

Please see picture attached.

enter image description here

Please see my code below.

    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']")));

The error log reads as:

    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)
Max Copley
  • 430
  • 3
  • 15

2 Answers2

0

This button is only showed up once you hover the code snippet with a mouse. In your example you just open the page.

See this question to learn how to make a mouse hover over the element: How to perform mouseover function in Selenium WebDriver using Java?

After that you will see "Copy" button displayed.

UPD:

Another thing that can cause your issue is that the element that is looked up with By.xpath("//a[text()='Copy']") is not the one you expect to have visible. Basically you are waiting for the visibility of the element that matches specified xpath fist in your DOM.

There are several snippets on the page and each has its "Copy" button. You might expect the button from second snippet to be visible however your waiter is awaiting the button from the first one.

Alexey R.
  • 8,057
  • 2
  • 11
  • 27
0

Here Copy button gets enable only when we hover the Code Snippet.

This can be achievable by using below code -

    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();

Here I am importing Actions class to implement the mouse action. After importing I am telling my action class to move to the code snippet section by first moveToElement() part and then move to the copy button, and then click the button.

For more details, you can go through the Mouse & Keyboard Action.