Reputation: 87
Trying to build out a webscraper but for the life of me I can't seem to get this link to click. I always get an Unable to Locate Element error no matter what I try. Here's the HTML:
<td colspan="2" width="100" height="100">
<a href="place.php?whichplace=desertbeach">
<img src="https://s3.amazonaws.com/images.kingdomofloathing.com/otherimages/main/map7beach.gif" alt="Desert Beach" title="Desert Beach" width="100" height="100" border="0">
</a>
</td>
I'm trying to click the link associated with the a href link. Tried find element by css, xpath and more but none can seem to find it. Any help?
Upvotes: 1
Views: 1113
Reputation: 33384
To click on a link induce WebDriverWait
and element_to_be_clickable
() and following CSS selector.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="place.php?whichplace=desertbeach"]'))).click()
OR XPATH
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[@href="place.php?whichplace=desertbeach"]'))).click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
UPDATE
The table is present inside and iframe you need to switch it first in order to click on the element.Induce WebDriverWait
() and frame_to_be_available_and_switch_to_it
() and use the css selector.
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, 'frame[src="main.php"]')))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[href="place.php?whichplace=desertbeach"]'))).click()
Upvotes: 1