是否有硒功能可以单击具有特定类的面积元素

时间:2018-12-31 07:13:14

标签: selenium selenium-chromedriver

我正在使用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. 

我想知道是否可以使用area元素单击图像。

任何能指出正确方向的想法都会很棒。谢谢。

2 个答案:

答案 0 :(得分:0)

根据您共享的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

答案 1 :(得分:0)

这是解决方案:

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

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