无法在所选元素上调用click方法

时间:2018-11-20 12:47:04

标签: python selenium selenium-webdriver xpath css-selectors

我需要单击Google MyBusiness页面中的特定按钮。问题是没有点击发生。

屏幕截图显示了应该被单击的按钮。元素的html是:

<div role="button" class="U26fgb zg hx mZ r2" jscontroller="VXdfxd" jsaction="click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue;touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd;focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;"
    jsshadow="" jsname="BddwAf" aria-label="Remove" aria-disabled="false" tabindex="0">
    <div class="Ci he" jsname="ksKsZd"></div>
    <content class="gx"><span style="top: -12px"><svg xmlns="https://www.w3.org/2000/svg" width="24" height="24"
                viewBox="0 0 24 24">
                <path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12z"></path>
                <path d="M0 0h24v24H0z" fill="none"></path>
            </svg></span></content>
</div>

我尝试使用以下选择器对相似元素进行广泛搜索:

hoursdiv = driver.find_elements_by_xpath('//div[@role = "button"]')
    for el in hoursdiv:
        print(el)
        el.click

输出:

<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-5")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-6")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-7")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-8")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-9")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-10")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-11")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-12")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-13")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-14")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-15")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-16")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-17")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-18")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-19")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-20")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-21")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-22")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-23")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-24")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-25")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-26")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-27")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-28")>
<selenium.webdriver.remote.webelement.WebElement (session="837341007f4b88350c3449e07e291d82", element="0.27450440234933726-29")>

但是该元素没有被点击。 显示这些元素的部分的html已上传here

enter image description here

3 个答案:

答案 0 :(得分:1)

尝试等到带有X按钮的模式窗口出现,并使用更具体的定位器选择按钮:

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

buttons = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div[arial-label="Remove"] svg')))
for button in buttons:
    button.click()

答案 1 :(得分:1)

您要单击的元素是启用了JavaScript的元素,因此您必须诱使 WebDriverwait 使元素可点击,并且您可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    hoursdiv = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div[role='button'][aria-label='Remove']")))
    for el in hoursdiv:
        print(el)
        el.click()
    
  • 使用XPATH

    hoursdiv = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@role='button' and @aria-label='Remove']")))
    for el in hoursdiv:
        print(el)
        el.click()
    
  • 注意:您必须添加以下导入:

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

答案 2 :(得分:0)

我认为等待不是您的问题,它需要单击两次div<content class="gx">