Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 硒“;硒.常见.例外.无接触元素例外“;当使用铬时_Python_Selenium_Selenium Webdriver_Webdriver_Nosuchelementexception_Java - Fatal编程技术网

Python 硒“;硒.常见.例外.无接触元素例外“;当使用铬时

Python 硒“;硒.常见.例外.无接触元素例外“;当使用铬时,python,selenium,selenium-webdriver,webdriver,nosuchelementexception,java,Python,Selenium,Selenium Webdriver,Webdriver,Nosuchelementexception,Java,我试图在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.52291

我试图在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

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

NoTouchElementException selenium.common.exceptions.NoSuchElementException通常称为
NoSuchElementException
,其定义如下:

exception selenium.common.exceptions.NoSuchElementException(msg=None, screen=None, stacktrace=None)
NoTouchElementException
基本上在以下两种情况下抛出:

elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].scrollIntoView();", elem)
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem.send_keys("text_to_send")
  • 使用时:

    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")
    
  • 使用时:

    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)
    

理由 NoTouchElementException的原因可能是以下任一原因:

  • 您所采用的定位器策略不会标识中的任何元素
  • 您采用的定位器策略无法识别该元素,因为它不在浏览器的位置范围内
  • 您采用的定位器策略标识元素,但由于存在属性style=“display:none;”,因此不可见
  • 您采用的定位器策略不会唯一地标识HTML DOM中所需的元素,并且当前会找到其他隐藏/不可见的元素
  • 您试图查找的WebElement位于
    标记内
  • WebDriver实例甚至在WebElement在HTMLDOM中出现/可见之前就在寻找WebElement

解决方案 解决NoTouchElementException的解决方案可以是以下任一种:

  • 采用唯一标识所需WebElement的。您可以借助开发人员工具(Ctrl+Shift+I或F12)并使用元素检查器

    在这里,您将看到有关的详细讨论

  • 使用方法滚动元素以查看,如下所示:

    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].scrollIntoView();", elem)
    
    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.send_keys("text_to_send")
    
    在这里,您将看到有关的详细讨论

  • 如果元素具有属性style=“display:none;”,请通过
    executeScript()
    方法删除该属性,如下所示:

    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].scrollIntoView();", elem)
    
    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.send_keys("text_to_send")
    
  • 要检查元素是否在
    中,请通过以下任一方法向上遍历HTML以找到相应的
    标记和
    切换到()
    所需的iframe:

    driver.switch_to.frame("iframe_name")
    driver.switch_to.frame("iframe_id")
    driver.switch_to.frame(1) // 1 represents frame index
    
    在这里,您可以找到有关的详细讨论

  • 如果元素在HTML DOM中不存在/不可见,请按如下所示将其设置为适当的方法:

    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].scrollIntoView();", elem)
    
    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.send_keys("text_to_send")
    
    • 等待:

    • 等待:

    • 等待:


这个用例 您看到的是
NoTouchElementException
,因为id定位器没有唯一地标识画布。要识别画布并在其上单击(),您必须等待画布可单击,并使用以下代码块:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//canvas[@id='window1']"))).click()

参考文献 您可以在以下位置找到基于客户的相关讨论:

NoTouchElementException selenium.common.exceptions.NoSuchElementException通常称为
NoSuchElementException
,其定义如下:

exception selenium.common.exceptions.NoSuchElementException(msg=None, screen=None, stacktrace=None)
NoTouchElementException
基本上在以下两种情况下抛出:

elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].scrollIntoView();", elem)
elem = driver.find_element_by_xpath("element_xpath")
driver.execute_script("arguments[0].removeAttribute('style')", elem)
elem.send_keys("text_to_send")
  • 使用时:

    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")
    
  • 使用时:

    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)
    

理由 NoTouchElementException的原因可能是以下任一原因:

  • 您所采用的定位器策略不会标识中的任何元素
  • 您采用的定位器策略无法识别该元素,因为它不在浏览器的位置范围内
  • 您采用的定位器策略标识元素,但由于存在属性style=“display:none;”,因此不可见
  • 您采用的定位器策略不会唯一地标识HTML DOM中所需的元素,并且当前会找到其他隐藏/不可见的元素
  • 您试图查找的WebElement位于
    标记内
  • WebDriver实例甚至在WebElement在HTMLDOM中出现/可见之前就在寻找WebElement

解决方案 解决NoTouchElementException的解决方案可以是以下任一种:

  • 采用唯一标识所需WebElement的。您可以借助开发人员工具(Ctrl+Shift+I或F12)并使用元素检查器

    在这里,您将看到有关的详细讨论

  • 使用方法滚动元素以查看,如下所示:

    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].scrollIntoView();", elem)
    
    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.send_keys("text_to_send")
    
    在这里,您将看到有关的详细讨论

  • 如果元素具有属性style=“display:none;”,请通过
    executeScript()
    方法删除该属性,如下所示:

    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].scrollIntoView();", elem)
    
    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.send_keys("text_to_send")
    
  • 要检查元素是否在
    中,请通过以下任一方法向上遍历HTML以找到相应的
    标记和
    切换到()
    所需的iframe:

    driver.switch_to.frame("iframe_name")
    driver.switch_to.frame("iframe_id")
    driver.switch_to.frame(1) // 1 represents frame index
    
    在这里,您可以找到有关的详细讨论

  • 如果元素在HTML DOM中不存在/不可见,请按如下所示将其设置为适当的方法:

    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].scrollIntoView();", elem)
    
    elem = driver.find_element_by_xpath("element_xpath")
    driver.execute_script("arguments[0].removeAttribute('style')", elem)
    elem.send_keys("text_to_send")
    
    • 等待:

    • 等待:

    • 等待:


这个用例 您看到的是
NoSuchElementExce