I need help to click a specific Image on a Website. Its in an iFrame too.
The HTML:
<i class="icon-willi-vote fxf-do-multivote" data-mv-id="1622095851001a" data-mission-id="97d05b44d86fb83b">
Snapshot of HTML:
Snapshot of the iframe:
How can I use this button/image with selenium and can I do it with
driver.find_element(...)
I tried find_element by xpath, class and id, but i cant find the way to get.
Given the HTML:
<i class="icon-willi-vote fxf-do-multivote" data-mv-id="1622095851001a" data-mission-id="97d05b44d86fb83b">
To click() on the element you can use either of the following locator strategies:
Using css_selector:
driver.find_element(By.CSS_SELECTOR, "i.icon-willi-vote.fxf-do-multivote[data-mv-id][data-mission-id]").click()
Using xpath:
driver.find_element(By.XPATH, "//i[@class='icon-willi-vote fxf-do-multivote'][@data-mv-id and @data-mission-id]").click()
Ideally to click on the clickable element you need 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, "i.icon-willi-vote.fxf-do-multivote[data-mv-id][data-mission-id]"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//i[@class='icon-willi-vote fxf-do-multivote'][@data-mv-id and @data-mission-id]"))).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
The desired element is within an <iframe>
and if the values within the attributes data-mv-id="1622095851001a"
and data-mission-id="97d05b44d86fb83b"
remains constant throughout you can use either of the following locator strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src^='https://www.willi.aka.krone.at']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "i.icon-willi-vote.fxf-do-multivote[data-mv-id='1622095851001a'][data-mission-id='97d05b44d86fb83b']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://www.willi.aka.krone.at')]")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//i[@class='icon-willi-vote fxf-do-multivote'][@data-mv-id='1622095851001a' and @data-mission-id='97d05b44d86fb83b']"))).click()
You can find a couple of relevant discussions in: