python - 使用 Chrome 时 Selenium "selenium.common.exceptions.NoSuchElementException"

标签 python selenium selenium-webdriver webdriver nosuchelementexception

我正在尝试玩QWOP在 Chrome 上使用 Selenium,但我不断收到以下错误:

selenium.common.exceptions.NoSuchElementException: 
Message: no such element: Unable to locate element
{"method":"id","selector":"window1"
(Session info: chrome=63.0.3239.108
(Driver info: chromedriver=2.34.522913
(36222509aa6e819815938cbf2709b4849735537c), platform=Linux 4.10.0-42-generic x86_64)

使用以下代码时:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time

browser = webdriver.Chrome()
browser.set_window_size(640, 480)
browser.get('http://www.foddy.net/Athletics.html?webgl=true')
browser.implicitly_wait(10)

canvas = browser.find_element_by_id("window1")

canvas.click()

while (True):
    action = ActionChains(browser)
    action.move_to_element(canvas).perform()
    canvas.click()
    canvas.send_keys("q")

相同的代码在 Firefox 上运行良好,但因为我想使用 chrome 的功能在 headless 模式下运行 webgl 游戏,所以我无法真正切换到 Firefox。

有什么解决方法可以让它工作吗?

最佳答案

NoSuchElementException

selenium.common.exceptions.NoSuchElementException 俗称 NoSuchElementException定义为:

exception selenium.common.exceptions.NoSuchElementException(msg=None, screen=None, stacktrace=None)
NoSuchElementException基本上在2种情况下抛出如下:
  • 使用时:
    webdriver.find_element_by_*("expression")
    //example : my_element = driver.find_element_by_xpath("xpath_expression")
    
  • 使用时:
    element.find_element_by_*("expression")
    //example : my_element = element.find_element_by_*("expression")
    

  • 根据 API 文档,就像任何其他 selenium.common.exceptions , NoSuchElementException应包含以下参数:
  • 消息,屏幕,堆栈跟踪
        raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":".//*[@id='create-portal-popup']/div[4]/div[1]/button[3]"}
      (Session info: chrome=61.0.3163.100)
      (Driver info: chromedriver=2.32.498550 (9dec58e66c31bcc53a9ce3c7226f0c1c5810906a),platform=Windows NT 10.0.10240 x86_64)
    


  • 原因

    NoSuchElementException 的原因可能是以下之一:
  • 您采用的定位器策略未识别 HTML DOM 中的任何元素.
  • 您采用的定位器策略无法识别该元素,因为它不在浏览器的 Viewport 内。 .
  • 您采用的定位器策略标识了元素,但由于属性 的存在而不可见style=“显示:无;” .
  • 您采用的定位器策略不是 独一无二标识 HTML DOM 中的所需元素,并且当前找到一些其他隐藏/不可见元素。
  • 您尝试定位的 WebElement 位于 <iframe> 中标签。
  • WebDriver 实例甚至在元素在 HTML DOM 中存在/可见之前就在寻找 WebElement。


  • 解决方案

    解决 NoSuchElementException 的解决方案可以是以下之一:
  • 采用Locator Strategy它唯一标识所需的 WebElement。您可以借助开发工具(Ctrl+Shift+I 或 F12)并使用 Element Inspector。

    在这里您可以找到关于 how to inspect element in selenium3.6 as firebug is not an option any more for FF 56? 的详细讨论。
  • 使用 execute_script() 滚动元素以查看的方法如下:
    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].scrollIntoView();", elem)
    

    在这里您可以找到关于 Scrolling to top of the page in Python using Selenium 的详细讨论。
  • Incase 元素具有属性 style=“显示:无;” ,通过executeScript()删除属性方法如下:
    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.send_keys("text_to_send")
    
  • 检查元素是否在 <iframe> 内遍历 HTML 以找到相应的 <iframe>标签和 switchTo()通过以下任一方法获得所需的 iframe:
    driver.switch_to.frame("iframe_name")
    driver.switch_to.frame("iframe_id")
    driver.switch_to.frame(1) // 1 represents frame index
    

    在这里你可以找到关于 How can I select a html element no matter what frame it is in in selenium? 的详细讨论。 .
  • 如果元素在 HTML DOM 中不存在/不可见,则诱导 WebDriverWaitexpected_conditions设置为正确的方法如下:
  • 等待presence_of_element_located :
    element = WebDriverWait(driver, 20).until(expected_conditions.presence_of_element_located((By.XPATH, "element_xpath']")))
    
  • 等待visibility_of_element_located :
    element = WebDriverWait(driver, 20).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "element_css")
    
  • 等待element_to_be_clickable :
    element = WebDriverWait(driver, 20).until(expected_conditions.element_to_be_clickable((By.LINK_TEXT, "element_link_text")))
    


  • 这个用例

    您正在查看 NoSuchElementException因为 id 定位器不能识别 Canvas 独一无二。识别 Canvas 和click()在它上面你必须等待 Canvas 成为 clickable为了实现这一点,您可以使用以下代码块:
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//canvas[@id='window1']"))).click()
    

    引用

    您可以找到Selenium基于客户的相关讨论:
  • NoSuchElementException, Selenium unable to locate element
  • 关于python - 使用 Chrome 时 Selenium "selenium.common.exceptions.NoSuchElementException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63262440/

    相关文章:

    php - 您对 PHP Web 应用程序的集成测试有何建议?

    java - 随机 "Element is no longer attached to the DOM"StaleElementReferenceException

    java - 如果它是两个组的成员,是否可以为 TestNG 设置一个条件来运行测试?

    c# - 如何在 Selenium [C#] 中添加单个等待两个不同的元素

    c# - Selenium WebDriver.ChromeDriver Nuget 包已安装,但不适用于 MSTest

    java - 断言错误(TestNG)使用java捕获问题

    python - TDD实践: Distinguishing between genuine failures and unimplemented features

    python - sqlite python INSERT IF NOT EXIST 有很多变量

    python - 无法将验证码对象转换为 base64

    python - 有什么方法可以替换 pandas pd.merge 吗?