2017-06-05 55 views
0

我一直在努力点击一个按钮使用量角器。问题是,即使驱动程序发现元素,但它仍然不会单击它。任何帮助是提前感谢。量角器无法点击按钮,即使它发现它

下面是按钮为标志的HTML:

div class="row mod-form-button"> 
 
       <button id="sign_in" class="mod-main" tabindex="4">Sign in</button> 
 
      </div> 
 
     <div class="row"> 
 
      <div class="row"> 
 
        <span class="nowrap">Not a member yet?</span> 
 
        <a class="nowrap" data-component="spinner" id="create_account" 
 
         href="create_account?client_id=UDPWeb1&callback=https%3A%2F%2F-na1-stg1.login.com"> 
 
         Get an Test ID</a> 
 
       </div>

量角器代码: 这是规范文件: // spec.js

describe(' Demo', function() { 

    it('Should Login to Demo Site', function() { 
    browser.waitForAngularEnabled(false); 
    browser.get('https://stage.com'); 



    // var EC = protractor.ExpectedConditions; 
// Waits for the element with id 'abc' to be clickable. 
//browser.wait(EC.elementToBeClickable($('password')), 5000); 
    element(by.id('password')).sendKeys('123456'); 
    element(by.id('username')).sendKeys('[email protected]'); 
    //browser.sleep(35000); 

    var EC = protractor.ExpectedConditions; 
// Waits for the element with id 'abc' to be clickable. 
element(by.id('sign_in')).click(); 
browser.sleep(5000); 
var el = element(by.id('sign_in')); 
browser.executeScript('arguments[0].scrollIntoView()', el.getWebElement()); 

el.click(); 
// browser.wait(EC.elementToBeClickable($('sign_in')), 5000); 
//browser.wait(EC.elementToBeClickable($('.btn.coral-btn.coral-btn-primary')), 5000); 
//element(by.css('.btn.coral-btn.coral-btn-primary')).click(); 
    // expect(element(by.binding('latest')).getText()). 
     // toEqual('5'); // This is wrong! 
    }); 
}); 

这是配置文件: //Config.js

exports.config = { 

    framework: 'jasmine', 
    seleniumAddress: 'http://localhost:4444/wd/hub', 
    allScriptsTimeout: 5000000, 
    specs: ['specs.js'] 


} 

回答

0

我已经使用Selenium-java进行了扩展,并测试了它的前端。 我用来测试的步骤和你的想法一样。

使用单击按钮,检查后存在。 (有时候点击evt()会比检查早),所以当它试图点击按钮时,按钮还不存在。

你的情况,我会做这样的步骤:

  1. 使用ExpectedCondition检查页面已经被加载。 ExpectedConditions.until(visibilityOf(WebElement))。推入元素 像输入(日志或通过)。
  2. SendKeys用于输入。比你可以检查input.value
  3. 按钮点击检查,这是存在和可点击。
  4. by.id(btnID).click()。

我希望这会有所帮助。

+0

感谢塔拉斯,我已经使用了上述解决方案,它不work.Also该元素存在,否则它会给出和元素不存在异常。 – Rahul

0

你的代码很混乱,你实际上声明了protractor.ExpectedConditions,但是你从不以正确的方式使用它。

然后要小心,因为所有的量角器方法都会返回promise,因为所有的操作都是异步的。您应该正确使用承诺。

顺便说一句,试试这个代码,它应该等待元素是可见的,然后单击它:

var EC = protractor.ExpectedConditions; 
var signinButton = element(by.id('sign_in')); 
// waiting for 10 seconds that the button will be visible 
browser.wait(EC.visibilityOf(signinButton), 10000, "Button signin not visible"); 
    browser.executeScript('document.querySelector("#sign_in").scrollIntoView()').then(function() { 
    signinButton.click().then(function() { 
    console.log('button has been clicked here'); 
    }); 
}); 
+0

谢谢,但它也不工作:'[17:14:08] E/launcher - Error:/Users/rtripath/Documents/protactor/specs.js:23 browser.executeScript('document.getElementByXpath('语法错误:无效或意外的令牌' – Rahul

+0

对不起,我连接了报价不好,请检查我目前的编辑 – quirimmo

+0

感谢您的帮助@quirimmo。但这仍然不会没有帮助,它有同样的问题,只是跳过点击和传递。参见下面链接中的图像:https://ibb.co/f1hjca – Rahul