【问题标题】:Message: no such element: Unable to locate element Selenium Python消息:没有这样的元素:无法找到元素 Selenium Python
【发布时间】:2020-07-15 11:04:11
【问题描述】:

我试图单击一个按钮,但收到以下错误消息: 我尝试单击的元素确实存在于页面上,但我不确定为什么它说该元素不存在:

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class="vote_button mfp-voteText"]"}

下面是我的代码:

driver.get('https://trupanion.com/canada/members/contest?pixlee_album_photo_id=427049511')

time.sleep(10)

try:
    vote = driver.find_element_by_xpath('//button[@class="vote_button mfp-voteText"]')
    vote.click()
except Exception as e:
    print(e)

下面是 chrome 开发工具中的 XPath,它显示它是正确的:

【问题讨论】:

  • 可能是因为网页没有加载?我看到错误:错误 1020 此网站正在使用安全服务来保护自己免受在线攻击。
  • 元素已加载。我确实在错误后检查了它,但它就在那里。有什么解决办法吗?

标签: python selenium selenium-webdriver iframe webdriverwait


【解决方案1】:

所需的元素在 <iframe> 内,因此您必须:

  • 诱导WebDriverWait 使所需的帧可用并切换到它

  • 诱导WebDriverWait 使所需的元素可点击

  • 您可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

    driver.get('https://trupanion.com/members/contest?pixlee_album_photo_id=427049511')
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#pixlee_lightbox_iframe")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.vote_button.mfp-voteText"))).send_keys("test")
    
  • 使用XPATH

    driver.get("https://trupanion.com/members/contest?pixlee_album_photo_id=427049511")
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='pixlee_lightbox_iframe']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='vote_button mfp-voteText']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照:


参考

您可以在以下位置找到一些相关讨论:

【讨论】:

  • 哈哈.. @DebanjanB。是你。我真的认不出你了。
  • 怎么样???在某些时候,我得到了您的帮助,并在抓取和硒领域参考了您的许多问题和答案。所以我只知道你的脸,不记得你的名字。
  • @jis0324 听起来不错,请继续访问Selenium Chat Room,我们经常在那里 :)
  • 你伪装成狗,但我能看到一张真脸。请注意,我拿着一把长剑。大声笑........
【解决方案2】:

尝试在 WebdriverWait() 函数中使用 css selector 而不是 XPATH。它将等待 X 秒以使元素可单击,并在元素出现时立即单击它。但是,您需要切换到必须通过frame 选择器找到的框架。

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

self.webdriver.switch_to_frame(self.webdriver.find_element_by_css_selector('frame'))
try:
    WebDriverWait(webdriver,time).until(EC.element_to_be_clickable((By.CSS_SELECTOR,path)))
except Exception as e:
   print(e)

【讨论】:

    【解决方案3】:

    <frame>标签内,先切换:

    driver.get('https://trupanion.com/canada/members/contest?pixlee_album_photo_id=427049511')
    
    time.sleep(10)
    
    try:
        #switch it first
        driver.switch_to.frame(driver.find_element_by_id('pixlee_lightbox_iframe'))
        vote = driver.find_element_by_xpath('//button[@class="vote_button mfp-voteText"]')
        vote.click()
    except Exception as e:
        print(e)
    

    但请注意time.sleep(..) 是个坏主意。

    你可以在这里学习 selenium 等待:

    selenium-python.readthedocs.io/waits.html

    对于切换帧:

    .frame_to_be_available_and_switch_to_it

    虽然你的 xpath 可以工作,但 css 选择器看起来更好:

    vote = driver.find_element_by_css_selector('button.vote_button.mfp-voteText')
    

    【讨论】:

      猜你喜欢
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-07
      • 1970-01-01
      相关资源
      最近更新 更多