0

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"

I have tried multiple "find element by" methods however I keep getting errors such as no such element.

button = driver.find_elements_by_class_name('toolbar-item') #not working
driver.findElementByClassName("a.cc_btn.cc_btn_accept_all") #not working
driver.find_element_by_css_selector("toolbar").click() #not working
driver.findElementByClassName("toolbar-item").click() #not working

In this case how to I select the "Copy" ref and what method is used to click "Copy"? Further, when the "Copy" button is clicked, how can I paste the contents to a text file.

These are the other errors I get.

Errors:

Traceback (most recent call last):
  File "untitled.py", line 27, in <module>
    driver.find_element_by_css_selector("toolbar-item").click()
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 598, in find_element_by_css_selector
    return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"toolbar-item"}
  (Session info: chrome=81.0.4044.138)

Also:

NoSuchElementException: Message: Unable to locate element:

enter image description here

1 Answer 1

1

To click on the Copy button induce WebDriverWait() and wait for presence_of_element_located() and following xpath.You need to scroll the element to click.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.w3resource.com/java-exercises/basic/java-basic-exercise-249.php")
copybtn=WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,"//a[text()='Copy']")))
copybtn.location_once_scrolled_into_view
copybtn.click()

You can use following css selector as well.

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://www.w3resource.com/java-exercises/basic/java-basic-exercise-249.php")
copybtn=WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR,".toolbar-item>a")))
driver.execute_script("arguments[0].scrollIntoView();", copybtn)
copybtn.click()
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Would you be able to post the Java equivalent to click the "Copy" button? :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.