0

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:

HTML Code

Snapshot of the iframe: 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.

Daniel R
  • 3
  • 2

2 Answers2

0

You can try finding the element with css selector and class names:

element = driver.find_element(By.CSS_SELECTOR, ".icon-willi-vote.fxf-do-multivote")

If the element is found successfully you can then .click() on the element or perform other operations.

jlhuhn
  • 1
  • 2
  • There are more .icon-willi-vote.fxf-do-multivote Buttons on the Homepage, i need a way to include the specific Button/Icon with: data-mv-id="1622095851001a" data-mission-id="97d05b44d86fb83b" – Daniel R Jun 07 '23 at 18:54
0

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
    

Update

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()
    

Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thx, but there are several ".icon-willi-vote.fxf-do-multivote" Buttons on the Homepage, i need a way to include the specific Button/Icon with: data-mv-id="1622095851001a" data-mission-id="97d05b44d86fb83b" Especially the Numbers, so the Program does not click the wrong Button. Thats the only way to get the specific Button. I can send u the whole Website, if u want – Daniel R Jun 07 '23 at 18:57
  • @DanielR The values within _`data-mv-id="1622095851001a"`_ and _`data-mission-id="97d05b44d86fb83b"`_ are dynamically generated. So we need to uniquely identify them relating to it's ancestors. Update the question with then text based HTML including the ancestor elements. – undetected Selenium Jun 07 '23 at 19:00
  • As i know, these 2 Data Values stayed the same all day long an in every Browser i tested, there is no other way to uniquely identify the button in the backend. here is the website: https://www.krone.at/3024904 – Daniel R Jun 07 '23 at 19:07
  • @DanielR Checkout the answer update and let me know the status. – undetected Selenium Jun 07 '23 at 20:47
  • I think the problem is a iframe, the button/image is in an iframe. I uploaded a Image of the iFrame. – Daniel R Jun 08 '23 at 10:40
  • @DanielR Yes, you are correct, I presumed you already taken care of the iframe early in your code. Checkout the answer update and let me know the status. – undetected Selenium Jun 08 '23 at 10:48
  • Thanks mate, it works now exactly like i want!! Really nice job, have a nice day! – Daniel R Jun 08 '23 at 11:31
  • @DanielR [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) – undetected Selenium Jun 08 '23 at 11:33