Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.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
Java 使用selenium webdriver选择按钮时出现问题_Java_Selenium Webdriver_Xpath_Css Selectors_Webdriverwait - Fatal编程技术网

Java 使用selenium webdriver选择按钮时出现问题

Java 使用selenium webdriver选择按钮时出现问题,java,selenium-webdriver,xpath,css-selectors,webdriverwait,Java,Selenium Webdriver,Xpath,Css Selectors,Webdriverwait,我正在尝试用不同的方法使用seleninum webdriver和java来选择一个特定的按钮,但不幸的是,什么都不起作用 当我使用Selenium IDE进行测试时,它正在工作。例如,我复制了相同的xpath,但当我尝试在java应用程序中进行测试时,什么都不起作用。我尝试使用不同的方法,By.cssSelector和By.path 这是我的html: <section class="fd-section"><fd-action-bar><div class="f

我正在尝试用不同的方法使用seleninum webdriver和java来选择一个特定的按钮,但不幸的是,什么都不起作用

当我使用Selenium IDE进行测试时,它正在工作。例如,我复制了相同的xpath,但当我尝试在java应用程序中进行测试时,什么都不起作用。我尝试使用不同的方法,By.cssSelector和By.path

这是我的html:

<section class="fd-section"><fd-action-bar><div class="fd-action-bar"><fd-action-bar-header class="fd-action-bar__header"><fd-action-bar-title><h1 class="fd-action-bar__title"> Applications </h1></fd-action-bar-title></fd-action-bar-header><fd-action-bar-actions class="fd-action-bar__actions"><y-list-search _nghost-c4="" hidden=""><!----><!----><div _ngcontent-c4="" clickoutsideevents="click,mousedown" excludebeforeclick="true" class="ng-star-inserted"><!----><button _ngcontent-c4="" fd-button="" class="fd-button xyz-icon--search fd-button--light ng-star-inserted"></button><!----></div></y-list-search><y-list-filter _nghost-c5="" hidden=""><!----></y-list-filter><!----><button class="open-create-namespace-modal fd-button xyz-icon--add ng-star-inserted" fd-button=""> Create Application </button></fd-action-bar-actions></div></fd-action-bar></section>
应用程序创建应用程序
我需要选择文本为“创建应用程序”的按钮

当我使用Selenium IDE创建测试时,此按钮的xpath是:

//按钮[包含(,“创建应用程序”)]

基本上,我的java代码是:

public WebElement wElement; wElement = driver.findElement(By.xpath("//button[contains(.,' Create Application')]")); wElement.click(); 公共网络元素; wElement=driver.findElement(By.xpath(//button[contains(,'Create Application')]); wElement.click(); 这是异常消息:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(.,' Create Application')]"} (Session info: chrome=76.0.3809.100) (Driver info: chromedriver=72.0.3626.69 (3c16f8a135abc0d4da2dff33804db79b849a7c38),platform=Mac OS X 10.14.6 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z' System info: host: 'C02WW0BZHTD8', ip: 'fe80:0:0:0:8f6:17e1:1a28:1e23%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.14.6', java.version: '1.8.0_171' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 72.0.3626.69 (3c16f8a135abc..., userDataDir: /var/folders/2r/99nyn7t16cz...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:60374}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 76.0.3809.100, webStorageEnabled: true} Session ID: b2341899cd9b62b0169b02371aaa3018 *** Element info: {Using=xpath, value=//button[contains(.,' Create Application')]} org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“方法”:“xpath”,“选择器”:“//button[包含(,“创建应用程序”)]”} (会话信息:chrome=76.0.3809.100) (驱动程序信息:chromedriver=72.0.3626.69(3C16F8A135ABC0D4DA2DF33804DB79B849A7C38),平台=Mac OS X 10.14.6 x8664)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时:0毫秒 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html 构建信息:版本:'3.14.0',修订版:'aacccce0',时间:'2018-08-02T20:19:58.91Z' 系统信息:主机:'C02WW0BZHTD8',ip:'fe80:0:0:0:8f6:17e1:1a28:1e23%en0',os.name:'Mac os X',os.arch:'x86_64',os.version:'10.14.6',java.version:'1.8.0_171' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能{AcceptInsureCerts:false,acceptSslCerts:false,applicationCacheEnabled:false,browserConnectionEnabled:false,browserName:chrome,chrome:{chromedriverVersion:72.0.3626.69(3c16f8a135abc…,userDataDir:/var/folders/2r/99nyn7t16cz…},CSS选择器Enabled:true,databaseEnabled:false,goog:chromeOptions:{debuggerAddress:localhost:60374},handlesAlerts:true,hasTouchScreen:false,javascriptEnabled:true,locationContextEnabled:true,mobileEmulationEnabled:false,nativeEvents:true,networkConnectionEnabled:false,pageLoadStrategy:normal,platform:MAC,platformName:MAC,proxy:proxy(),可旋转:false,setWindowRect:true,takesHeapSnapshot:true,takesScreenshot:true,超时:{implicit:0,pageLoad:300000,script:30000},意外行为:忽略,未处理PromptBehavior:ignore,版本:76.0.3809.100,WebStorage Enabled:true} 会话ID:b2341899cd9b62b0169b02371aaa3018 ***元素信息:{Using=xpath,value=//按钮[包含(,'Create Application')]}
当执行这段代码时,按钮是否已加载到页面中

{implicit:0,pageLoad:300000,script:30000}
表明驱动程序不会隐式等待查找任何元素。即,如果元素不可用,它将立即引发异常

在尝试查找按钮之前,请尝试
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Create Application')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver; 
executor.executeScript("arguments[0].click();", element);
也可以尝试使用以下定位器-

  • //按钮[包含(,'Create Application')]
  • //按钮[包含(text(),'Create Application')]
  • 如果以上都不起作用,请您提供一个URL(如果是公共的)


    还要检查按钮是否在框架内。

    执行此代码时,按钮是否已加载到页面中

    {implicit:0,pageLoad:300000,script:30000}
    表明驱动程序不会隐式等待查找任何元素。即,如果元素不可用,它将立即引发异常

    在尝试查找按钮之前,请尝试
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

    WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Create Application')]"));
    JavascriptExecutor executor = (JavascriptExecutor)driver; 
    executor.executeScript("arguments[0].click();", element);
    
    也可以尝试使用以下定位器-

  • //按钮[包含(,'Create Application')]
  • //按钮[包含(text(),'Create Application')]
  • 如果以上都不起作用,请您提供一个URL(如果是公共的)


    还要检查按钮是否在框架内。

    在这种情况下,您可以使用JavascriptExecutor界面并尝试单击按钮

    WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Create Application')]"));
    JavascriptExecutor executor = (JavascriptExecutor)driver; 
    executor.executeScript("arguments[0].click();", element);
    
    还可以尝试使用WebDriverWait

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Create Application')]"))).click();
    

    在这种情况下,您可以使用JavascriptExecutor接口并尝试单击按钮

    WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Create Application')]"));
    JavascriptExecutor executor = (JavascriptExecutor)driver; 
    executor.executeScript("arguments[0].click();", element);
    
    还可以尝试使用WebDriverWait

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Create Application')]"))).click();
    
    所需的元素是一个元素,因此要定位该元素,您必须为
    elementtobelickable()
    引入WebDriverWait,并且可以使用以下任一选项:

    • css选择器

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("fd-action-bar-actions.fd-action-bar__actions button.open-create-namespace-modal.fd-button.xyz-icon--add.ng-star-inserted"))).click();
      
    • xpath

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='open-create-namespace-modal fd-button xyz-icon--add ng-star-inserted' and contains(., 'Create Application')]"))).click();
      
    其他考虑事项 确保:

    • JDK已升级到当前级别
    • 硒被提升到当前水平
    • ChromeDriver已更新到当前级别
    • Chrome更新至当前Chrome版本76.0级别。(根据)
    在这里,您可以找到有关的详细讨论

    所需的元素是一个元素,因此要定位该元素,您必须为
    elementtobelickable()
    引入WebDriverWait,并且可以使用以下任一选项:

    • css选择器

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("fd-action-bar-actions.fd-action-bar__actions button.open-create-namespace-modal.fd-button.xyz-icon--add.ng-star-inserted"))).click();
      
    • xpath

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='open-create-namespace-modal fd-button xyz-icon--add ng-star-inserted' and contains(., 'Create Application')]"))).click();
      
    其他考虑事项 确保:

    • JDK已升级到当前级别
    • 硒被提升到当前水平
    • ChromeDriver已更新到当前级别
    • Chrome更新至当前Chrome版本76.0级别。(根据)
    在这里,您可以找到有关的详细讨论


    您好Sachin,非常感谢您的回复!是的,该按钮在页面加载时可用。无论如何,我添加了“driver.manage()…”并按照您的提示进行操作,但不幸的是还没有工作。这非常奇怪,因为我遵循相同的过程