无法使用webdriverio单击Web按钮

时间:2015-12-12 17:10:27

标签: javascript jquery html node.js webdriver-io

我现在正尝试在nodejs中使用webdriverio学习自动化,但事实上并不是HTML中jQuery最多的知识。我在哪里:

我写了一个脚本,到目前为止成功登录我的帐户并导航到我想要的页面。我的目标是进行预订 - 但是当我点击预订项目时,会出现一个弹出窗口,其中包含一些预先加载的数据以及" make reservation"按钮,点击运行javascript函数。但是,我还没有能够成功重新创建实际点击..

按钮的HTML如下所示:

<div class="inside">
            <ul class="toolBtns">

                <li id="ctl00_ctrl_MakeBookingTime_liCancelChanges" class="cancelChanges">
                    <a id="ctl00_ctrl_MakeBookingTime_lbCancelChanges" href="javascript:__doPostBack('ctl00$ctrl_MakeBookingTime$lbCancelChanges','')">Discard Changes</a></li>
                <li id="ctl00_ctrl_MakeBookingTime_liBook" class="bookRes">
                    <a id="ctl00_ctrl_MakeBookingTime_lbBook" href="javascript:__doPostBack('ctl00$ctrl_MakeBookingTime$lbBook','')">Make Reservation</a></li>
            </ul>               
        </div>

到目前为止,我已尝试使用此功能点击它(这是我的&#34;客户端&#34;声明后的代码)

  .execute(executeFunction).then(function(resp){
  console.log('execute function status:',resp.state) // should log true if element is there
})
.waitForExist('#ctl00_ctrl_MakeBookingTime_lbBook').then(function(exists){
  console.log('button exists status:',exists.state) // should log true if element is there
})
.elements('#ctl00_ctrl_MakeBookingTime_lbBook').then(function(resp){
    console.log('element found status: ',resp);
  return; //wait for the popup to load before executing the click
}).click('=Make Reservation') //this is the line that doesn't seem to work

最后一行是不起作用的。如您所见,我使用正则表达式作为查询元素,但我也直接按照建议here尝试了项目ID。我还尝试在HREF链接中手动执行javascript函数,但这只是在没有实际预订的情况下解除窗口:

.execute("javascript:__doPostBack('ctl00$ctrl_MakeBookingTime$lbBook")

我猜_doPostBack函数中有一些参数,我不理解..

任何建议或其他文章的链接都会非常有帮助。我已经淘了好几个小时了,无法弄明白。谢谢!

更新

:ChristianB要求提供日志,这里是:

2:32:16.747 INFO - Done: [execute script: javascript:LaunchLockedReserver(this,event,'54','200','12/19/2015','9:15 AM','2');, []]
12:32:16.752 INFO - Executing: [find elements: By.xpath: //*[contains(@id, "ctl00_ctrl_MakeBookingTime_lbBook") and contains(., "Make Reservation")]])
12:32:16.766 INFO - Done: [find elements: By.xpath: //*[contains(@id, "ctl00_ctrl_MakeBookingTime_lbBook") and contains(., "Make Reservation")]]
12:32:17.023 INFO - Executing: [find elements: By.xpath: //*[contains(@id, "ctl00_ctrl_MakeBookingTime_lbBook") and contains(., "Make Reservation")]])
12:32:17.038 INFO - Done: [find elements: By.xpath: //*[contains(@id, "ctl00_ctrl_MakeBookingTime_lbBook") and contains(., "Make Reservation")]]
12:32:17.303 INFO - Executing: [find elements: By.xpath: //*[contains(@id, "ctl00_ctrl_MakeBookingTime_lbBook") and contains(., "Make Reservation")]])
12:32:17.303 INFO - Executing: [find element: By.xpath: //*[contains(@id, "ctl00_ctrl_MakeBookingTime_lbBook") and contains(., "Make Reservation")]])
12:32:17.311 INFO - Done: [find elements: By.xpath: //*[contains(@id, "ctl00_ctrl_MakeBookingTime_lbBook") and contains(., "Make Reservation")]]
12:32:17.568 WARN - Exception thrown
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[contains(@id, "ctl00_ctrl_MakeBookingTime_lbBook") and contains(., "Make Reservation")]"}

似乎最后一块没有用。谢谢你指点我的日志。奇怪的是,当我将正则表达式直接更改为ID时,它仍会出现错误:

12:38:33.483 WARN - Exception thrown
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"ctl00_ctrl_MakeBookingTime_lbBook"}

我会继续尝试并查看日志

更新#2

我为我运行的每个函数登录了.then函数(参见上面的更新代码),似乎甚至都没有运行.waitForExists函数。运行该函数后,我的控制台输出如下所示:

execute function status: success
Error: element (#ctl00_ctrl_MakeBookingTime_lbBook) still not existing after 500ms
at elements("#ctl00_ctrl_MakeBookingTime_lbBook") - isExisting.js:43:17
at isExisting("#ctl00_ctrl_MakeBookingTime_lbBook") - waitForExist.js:31:21

基本上它似乎从.execute直接转到.elements函数......

1 个答案:

答案 0 :(得分:2)

您可能使用.waitForExist错误。

来自http://webdriver.io/api/utility/waitForExist.html。一旦ms用完,它的Future将返回一个布尔值,具体取决于它是否找到选择器。

.waitForExist(selector[,ms][,reverse]).then(callback);

msreverse是可选的。您需要默认值,在{wdio配置文件中设置ms

要验证是否找到该元素,您可以执行此操作,

waitForExist('#ctl00_ctrl_MakeBookingTime_lbBook').then(function(exists){ console.log(exists) // should log true if element is there })

您不应该尝试部分匹配“预订”#39;因为id选择器应该是唯一的。

<强>更新

期货受到束缚,很难说出未来拆分链的位置。有时候,当我无法在某些东西断裂的时候做出正面或反面时,我会在每个链式成员之间拨打电话,我知道会回复一些东西,比如

.getTitle().then(function(title){
    console.log(title);
});

属性exists.state不可用。虽然它不在文档中,但我已经验证了waitForExists解析为true或false。如果您不确定传入的参数,可以使用console.log(arguments)将参数作为数组获取。不要将其用于生产。尝试在waitForExist.then()回调中执行此操作以查看您拥有的内容。

你不应该elements(即使这可能不是问题的根源)。如果您只想点击一个按钮.click(#ctl00_ctrl_MakeBookingTime_lbBook),那么该怎么办呢。

您能详细描述以下代码吗?它也可能影响未来的其他链条。你能输出console.log(arguments)并发布吗?

.execute(executeFunction).then(function(resp){
  console.log('execute function status:',resp.state) // should log true if element is there
  console.log(arguments);
})