【问题标题】:Is there a selenium function to click on area element with a particular class是否有一个 selenium 函数可以单击具有特定类的区域元素
【发布时间】:2018-12-31 07:13:14
【问题描述】:

我正在使用 Python 开发 Selenium。我对此很陌生,我正在尝试单击具有以下 HTML 的图像

结构:

HTML:

<div id="result">
   <img class="img-450" usemap="#476872907" src="#" border="0" title="Click on a repeated (mirrored) image bellow" alt="">
   <map name="476872907">
      <area class="cursor" onclick="javascript:Captcha107('6k9frpw1');" title="Click on a repeated (mirrored) image bellow" coords="1,4,63,88">
      <area class="cursor" onclick="javascript:Captcha107('ya3ltbiv');" title="Click on a repeated (mirrored) image bellow" coords="65,4,128,88">
      <area class="cursor" onclick="javascript:Captcha107('9rbukmey');" title="Click on a repeated (mirrored) image bellow" coords="129,4,191,88">
      <area class="cursor" onclick="javascript:Captcha107('5mlv909r');" title="Click on a repeated (mirrored) image bellow" coords="192,4,255,88">
      <area class="cursor" onclick="javascript:Captcha107('eu7idjnz');" title="Click on a repeated (mirrored) image bellow" coords="257,4,319,88">
      <area class="cursor" onclick="javascript:Captcha107('al9a6zih');" title="Click on a repeated (mirrored) image bellow" coords="321,4,384,88">
      <area class="cursor" onclick="javascript:Captcha107('nyvkjeoc');" title="Click on a repeated (mirrored) image bellow" coords="385,4,448,88">
   </map>
</div>

我已经尝试了几件事,例如:

driver.find_element_by_xpath('//img/@class="img-450"').click()

driver.find_element_by_xpath('//area[@class="cursor"]').click()

但是,我似乎总是以错误告终,例如:

selenium.common.exceptions.WebDriverException: Message: unknown error: shape= is not supported

sselenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //img/@class="img-450" because of the following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type. 

我想知道是否可以使用区域元素以某种方式单击图像。

任何可以为我指明正确方向的想法都会很棒。谢谢。

【问题讨论】:

标签: selenium selenium-chromedriver


【解决方案1】:

根据你分享的 HTML,你可以试试这个代码:

wait = WebDriverWait(driver, 10)
img= wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div#result>img')))
img.click()  

确保导入这些:

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

【讨论】:

  • 我试过这个但遇到了以下错误:元素 在点 (468, 332) 处不可点击。其他元素会收到点击:
【解决方案2】:

这是解决方案:

driver.execute_script("javascript:Captcha107('6k9frpw1');")

使用 re 库获取源页面和此代码 ->('6k9frpw1')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 1970-01-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多