Python Image/Button click via selenium

huangapple go评论69阅读模式
英文:

Python Image/Button click via selenium

问题

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:

Python Image/Button click via selenium

Snapshot of the iframe:
Python Image/Button click via selenium

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 can't find the way to get.

英文:

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:

Python Image/Button click via selenium

Snapshot of the iframe:
Python Image/Button click via selenium

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.

答案1

得分: 0

你可以尝试使用 CSS 选择器和类名查找元素:
element = driver.find_element(By.CSS_SELECTOR, ".icon-willi-vote.fxf-do-multivote")
如果成功找到元素,你可以执行 .click() 操作或其他操作。

英文:

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.

答案2

得分: 0

给定以下HTML:

<i class="icon-willi-vote fxf-do-multivote" data-mv-id="1622095851001a" data-mission-id="97d05b44d86fb83b">

要点击此元素,您可以使用以下任一定位策略

  • 使用 css_selector
driver.find_element(By.CSS_SELECTOR, "i.icon-willi-vote.fxf-do-multivote[data-mv-id][data-mission-id]").click()
  • 使用 xpath
driver.find_element(By.XPATH, "//i[@class='icon-willi-vote fxf-do-multivote'][@data-mv-id and @data-mission-id]").click()

最好使用WebDriverWait来点击可点击的元素,您可以使用以下任一定位策略

  • 使用 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()
  • 使用 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()

注意:您需要添加以下导入语句:

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

更新:

所需的元素位于<iframe>中,如果属性data-mv-id=&quot;1622095851001a&quot;data-mission-id=&quot;97d05b44d86fb83b&quot;的值在整个过程中保持不变,您可以使用以下任一定位策略

  • 使用 CSS_SELECTOR
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,&quot;iframe[src^=&#39;https://www.willi.aka.krone.at&#39;]&quot;)))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, &quot;i.icon-willi-vote.fxf-do-multivote[data-mv-id=&#39;1622095851001a&#39;][data-mission-id=&#39;97d05b44d86fb83b&#39;]&quot;))).click()
  • 使用 XPATH
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,&quot;//iframe[starts-with(@src, &#39;https://www.willi.aka.krone.at&#39;)]&quot;))) 	
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//i[@class=&#39;icon-willi-vote fxf-do-multivote&#39;][@data-mv-id=&#39;1622095851001a&#39; and @data-mission-id=&#39;97d05b44d86fb83b&#39;]&quot;))).click()

参考:

您可以在以下讨论中找到一些相关的讨论:

英文:

Given the HTML:

&lt;i class=&quot;icon-willi-vote fxf-do-multivote&quot; data-mv-id=&quot;1622095851001a&quot; data-mission-id=&quot;97d05b44d86fb83b&quot;&gt;

To click() on the element you can use either of the following locator strategies:

  • Using css_selector:

    driver.find_element(By.CSS_SELECTOR, &quot;i.icon-willi-vote.fxf-do-multivote[data-mv-id][data-mission-id]&quot;).click()
    
  • Using xpath:

    driver.find_element(By.XPATH, &quot;//i[@class=&#39;icon-willi-vote fxf-do-multivote&#39;][@data-mv-id and @data-mission-id]&quot;).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, &quot;i.icon-willi-vote.fxf-do-multivote[data-mv-id][data-mission-id]&quot;))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//i[@class=&#39;icon-willi-vote fxf-do-multivote&#39;][@data-mv-id and @data-mission-id]&quot;))).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 &lt;iframe&gt; and if the values within the attributes data-mv-id=&quot;1622095851001a&quot; and data-mission-id=&quot;97d05b44d86fb83b&quot; 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,&quot;iframe[src^=&#39;https://www.willi.aka.krone.at&#39;]&quot;)))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, &quot;i.icon-willi-vote.fxf-do-multivote[data-mv-id=&#39;1622095851001a&#39;][data-mission-id=&#39;97d05b44d86fb83b&#39;]&quot;))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,&quot;//iframe[starts-with(@src, &#39;https://www.willi.aka.krone.at&#39;)]&quot;))) 	
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//i[@class=&#39;icon-willi-vote fxf-do-multivote&#39;][@data-mv-id=&#39;1622095851001a&#39; and @data-mission-id=&#39;97d05b44d86fb83b&#39;]&quot;))).click()
    

Reference

You can find a couple of relevant discussions in:

huangapple
  • 本文由 发表于 2023年6月8日 00:55:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76425551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定