0

I am making a script that will navigate through a website and perform some actions for me. But I am stuck trying to make my script click on the following elemnt:

<a href="JavaScript:OnCopy(22291488);" title="Copy Trip"><img src="../Images/copy.gif"></a>

As there is no ID or class name for this element, i tried to find the element by xpath and clicking on it by using the following code:

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

....

copy = driver.find_element_by_xpath('//[@id="divGrid"]/div[2]/div[2]/table/tbody/tr[1]/td[1]/div/span/a[2]')
copy.click()

This isn't working, so I am open to suggestions to how to solve this issue.

3
  • Can you supply more of your code and the url with the element you're trying to click on? Commented Jun 22, 2020 at 18:02
  • also provide code where you instantiate your driver. Commented Jun 22, 2020 at 18:25
  • //a[contains(@href, "OnCopy(22291488)")] Commented Jun 22, 2020 at 20:11

1 Answer 1

1

The desired element is a JavaScript enabled 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:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Copy Trip']>img[src$='/Images/copy.gif']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Copy Trip' and contains(@href, 'OnCopy')]/img[contains(@src, '/Images/copy.gif')]"))).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
    
Sign up to request clarification or add additional context in comments.

Comments

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.