英文:
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:
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 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:
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.
答案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="1622095851001a"
和data-mission-id="97d05b44d86fb83b"
的值在整个过程中保持不变,您可以使用以下任一定位策略:
- 使用 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()
- 使用 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()
参考:
您可以在以下讨论中找到一些相关的讨论:
- 通过Selenium和Python切换到iframe
- selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
- selenium in python : NoSuchElementException: Message: no such element: Unable to locate element
英文:
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:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论