【问题标题】:Problem to select a button using selenium webdriver使用 selenium webdriver 选择按钮的问题
【发布时间】:2019-08-21 14:03:33
【问题描述】:

我正在尝试使用 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 为:

//按钮[contains(.,'创建应用程序')]

基本上,我的java代码是:

公共WebElement wElement; wElement = driver.findElement(By.xpath("//button[contains(.,'创建应用程序')]")); wElement.click();

这是异常消息:

org.openqa.selenium.NoSuchElementException: 没有这样的元素: 无法定位元素: {"method":"xpath","selector":"//button[contains(.,'Create Application')]"} (会话信息:chrome=76.0.3809.100) (驱动程序信息:chromedriver=72.0.3626.69 (3c16f8a135abc0d4da2dff33804db79b849a7c38),platform=Mac OS X 10.14.6 x86_64)(警告:服务器未提供任何堆栈跟踪信息) 命令持续时间或超时: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 功能 {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 :正常,平台:MAC,平台名称:MAC,代理:Proxy(),可旋转:false,setWindowRect:true,takesHeapSnapshot:true,takeScreenshot:true,超时:{implicit:0,pageLoad:300000,脚本:30000},unexpectedAlertBehaviour :忽略,unhandledPromptBehavior:忽略,版本:76.0.3809.100,webStorageEnabled:true} 会话 ID:b2341899cd9b62b0169b02371aaa3018 *** 元素信息:{Using=xpath, value=//button[contains(.,'Create Application')]}

【问题讨论】:

    标签: java selenium-webdriver xpath css-selectors webdriverwait


    【解决方案1】:

    在执行这段代码时,按钮是否已经加载到页面中?

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

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

    还可以尝试使用以下定位器 -

    1. //button[contains(.,'Create Application')]
    2. //button[contains(text(),'Create Application')]

    如果以上方法均无效,请提供网址(如果是公开的)

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

    【讨论】:

    • 您好 Sachin,非常感谢您的回复!是的,该按钮在页面加载时可用。无论如何,我添加了“driver.manage()......”并且我遵循了你的提示,不幸的是还没有工作。这很奇怪,因为我在使用其他按钮时遵循相同的过程并且这是有效的,只是这个特定的按钮不起作用。
    【解决方案2】:

    这种情况下可以使用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();
    

    【讨论】:

    • 我尝试使用这两种情况,但不幸的是仍然无法正常工作。我尝试对 By.xpath 使用两种不同的语法:1. //button[contains(.,'Create Application')] 和 2. //button[contains(text(),'Create Application')] 这很奇怪因为当我使用 Selenium IDE 执行测试时,使用语法 2 单击按钮。当我在 java 源代码中使用相同的语法时不起作用。
    • 我收到了这个异常:org.openqa.selenium.TimeoutException: 预期条件失败:等待元素可点击:By.xpath: //button[contains(.,' Create Application')] (尝试 20 秒,间隔 500 毫秒)
    【解决方案3】:

    所需的元素是Angular 元素,因此要找到您必须为elementToBeClickable() 诱导WebDriverWait 的元素,您可以使用以下Locator Strategies 之一:

    • cssSelector:

      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();
      

    其他注意事项

    确保:

    在这里你可以找到关于NoSuchElementException, Selenium unable to locate element的详细讨论

    【讨论】:

    • 正在工作! :) 为了解决我需要使用它,因为按钮位于 iframe 内。 WebElement iFrame= driver.findElement(By.tagName("iframe")); driver.switchTo().frame(iFrame);
    • @Marcelo 您提供的 HTML 缺少 iframe 标记。即使是现有的 HTML 听起来也不像是在 iframe 中。因此很难猜测。很高兴您的问题得到解决。如果此答案符合您的问题,您可以通过单击答案旁边的空心复选标记来接受答案,该复选标记就在投票箭头下方,使复选标记变为绿色。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 2014-06-18
    • 2017-08-21
    • 1970-01-01
    相关资源
    最近更新 更多