2

i am trying to automate some translation through selenium using this url https://www.deepl.com/translator . However I am unable to click on copy button shown in photo here.red marking on button . Inspecting this reveals this html code

<button tabindex="130" _dl-connected="1" _dl-attr="onClick: $0.doCopy" _dl-attr-type="null">
                <svg width="20" height="22" viewBox="0 0 20 22" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M16.2949 15.7893H8.59364C7.36747 15.7893 6.37793 14.7947 6.37793 13.5623V3.22705C6.37793 1.9946 7.36747 1 8.59364 1H16.3164C17.5425 1 18.5321 1.9946 18.5321 3.22705V13.5839C18.5106 14.7947 17.521 15.7893 16.2949 15.7893Z" stroke-miterlimit="10"></path>
                    <path d="M11.1966 20.9997H3.34478C2.05408 20.9997 1 19.9402 1 18.6429V7.35629C1 6.05898 2.05408 4.99951 3.34478 4.99951H11.1966C12.4873 4.99951 13.5414 6.05898 13.5414 7.35629V18.6645C13.5414 19.9402 12.4873 20.9997 11.1966 20.9997Z" fill="white" stroke-miterlimit="10"></path>
                </svg>
            </button>

Please guide on how to locate this button using both xpath(what should be tag and attribute to be used here) and one other method say css locator. I would be obliged.

The code I used to try and locate the button is:

cpy_btn = driver.find_elements_by_xpath('//*[@id="dl_translator"]/div[1]/div[4]/div[3]/div[4]/div[1]/button')

And later I used

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//button[@tabindex='130'])")))

but both didn't work.

The error message I received is: selenium.common.exceptions.ElementClickInterceptedException: Message: Element <button> is not clickable at point (1177,601) because another element <p> obscures it

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • Could you show what you tried to do to click on it? My simple answer would be, copy the xpath by looking at the page code but I'm assuming you already tried that. – Jem Jun 13 '20 at 10:15
  • @Jem you are right, i tried that one but it didn't work , this was used cpy_btn = driver.find_elements_by_xpath('//*[@id="dl_translator"]/div[1]/div[4]/div[3]/div[4]/div[1]/button') and later I used WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//button[@tabindex='130'])"))) but both didn't. Message is selenium.common.exceptions.ElementClickInterceptedException: Message: Element – Mohammad Ahmed Jun 13 '20 at 10:23

2 Answers2

1

The desired element is a dynamic element so to click on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.deepl.com/translator')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.lmt__target_toolbar__copy > button"))).click()
    
  • Using XPATH:

    driver.get('https://www.deepl.com/translator')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='lmt__target_toolbar__copy']/button"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

deepl

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • I will try your solution also but i actually solved the problem by just getting value of text box instead of clicking on copy button. bttn=WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//textarea)[2]"))) translated_data=bttn.get_attribute('value') – Mohammad Ahmed Jun 14 '20 at 05:54
  • xpath did not work but css selector did, thanks for this – Mohammad Ahmed Jun 14 '20 at 06:03
0

You can try to add the following imports :

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

Then use :

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "(//textarea)[2]"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='lmt__target_toolbar__copy']/button"))).click()

Whith ActionChains :

from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='lmt__target_toolbar__copy']/button")))).click().perform()
E.Wiest
  • 5,122
  • 2
  • 4
  • 11