Reputation: 1
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.
Upvotes: 0
Views: 39
Reputation: 193058
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
Upvotes: 1