selenium-webdriver Javascript遇到的问题

要自动从某官方网站上自动下载某文件,结果因为password input的sendKeys一直无法执行,并报错element not visible或者提示

WebDriverError: element not interactable

经历几天重重磨难,发现主要是输入账户后,没有等待一段时间,Google搜索的时候stackoverflow也有给相关的建议,用了提示的方法不成功

  gmail.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  TypeError: driver.manage(...).timeouts is not a function

然后Google这个错误,得到了如下结果

const TIMEOUT = 300000000
driver.manage().setTimeouts( { implicit: TIMEOUT, pageLoad: TIMEOUT, script: TIMEOUT } )

是写给python语言的,跟js也差不多,也给出了一个官方链接的说明文档:seleniumhq.github.io/selenium/docs/api/javascript

Selenium source code : https://github.com/SeleniumHQ/selenium

===================以上是答案,以下是乱扯==============
之前在好多网站上看到的如下方法:

 WebDriverWait wait = new WebDriverWait(driver, 10);

表示这个方法并不支持Javascript语言,而是Java语言的,我是从另一个官方网站上的example确定这点的~~

Java:


Java example from docs.seleniumhq.org 2018-12-28 at 13.37.51.png

Javascript:


JS example from docs.seleniumhq.org 2018-12-28.png

下次查找相关解决方法时,可以注意下对应解决办法是否支持当前所使用的语言;而且发现下面语句的ExpectedConditions好像也不支持JS,适用了各种办法,并没有成功。

  try {
   wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(btnMenu)));
} catch (Exception e) {
    System.out.println("Oh");

//这两个方法也是不可用的
WebDriverWait wait = new WebDriverWait(gmail, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));

还有Actions的解决办法好像也不成功,GitHub上说时不支持node.js,w3c不支持

selenium-web-driver-java-element-is-not-clickable-at-point-x-y这个链接的方法可能只支持Java语言的,下面为其中某个回答,答案不一定准确适用你当前使用的语言和版本,仅做记录和参考:

Not Clickable Solution

The error Element is not clickable at point (x, y) can arise from different factors. You can address them by either of the following procedures:

1. Element not getting clicked due to JavaScript or AJAX calls present

Try to use Actions Class:

WebElement element = driver.findElement(By.id("navigationPageButton"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

2. Element not getting clicked as it is not within Viewport

Try to use JavascriptExecutor to bring the element within the Viewport:

WebElement myelement = driver.findElement(By.id("navigationPageButton"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement); 

3. The page is getting refreshed before the element gets clickable.

In this case induce ExplicitWait i.e WebDriverWait as mentioned in point 4.

4. Element is present in the DOM but not clickable.

In this case induce ExplicitWait with ExpectedConditions set to elementToBeClickable for the element to be clickable:

WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));

5. Element is present but having temporary Overlay.

In this case induce ExplicitWait with ExpectedConditions set to invisibilityOfElementLocated for the Overlay to be invisible.

WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));

6. Element is present but having permanent Overlay.

Use JavascriptExecutor to send the click directly on the element.

WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);

Remove an attribute

Set the element's disabled property to false:

document.getElementById('my-input-id').disabled = false;

If you're using jQuery, the equivalent would be:

 $('#my-input-id').prop('disabled', false);

For several input fields, you may access them by class instead:

var inputs = document.getElementsByClassName('my-input-class');
for(var i = 0; i < inputs.length; i++) {
   inputs[i].disabled = false;
}

Where document could be replaced with a form, for instance, to find only the elements inside that form. You could also use getElementsByTagName('input') to get all input elements. In your for iteration, you'd then have to check that inputs[i].type == 'text'.

为了应对JS不同的框架:

1. vanilla JS: elem.removeAttribute('disabled')
2. jQuery: elem.removeAttr('disabled')
jQuery("#success").removeAttr("disabled");

To set the disabled to false using the name property of the input:

document.myForm.myInputName.disabled = false;

Change an element's class

document.getElementById("MyElement").classList.add('MyClass');

document.getElementById("MyElement").classList.remove('MyClass');

if (document.getElementById("MyElement").classList.contains('MyClass') )

 document.getElementById("MyElement").classList.toggle('MyClass');

上篇答案有考虑到addEventListener和不同框架主要是JQuery之间的差异
有篇中文博客也不错:用JS添加和删除class类名

遗留下的问题:

  1. inline mode和external js mode有什么区别吗?

  2. 关于Actions,Javascript版的好像不可用,GitHub上有这个讨论,下面这段代码也会报错

    UnknownCommandError: Unrecognized command: actions
    

    ======================================

    const {Builder, By, Key, until} = require('selenium-webdriver');
    
    (async function example() {
     let driver = await new Builder().forBrowser('chrome').build();
     var entrytoEdit = "Browser Stack";
     var toDoTestItems = [entrytoEdit, "Test Value1", "Test Value2"];
     await driver.get('http://todomvc.com/examples/react/#/');
     let query = await  driver.wait(until.elementLocated(By.className('new-todo')),1000)
     for (const item of toDoTestItems) {
     await query.sendKeys(item, Key.RETURN);
    }
      const deleteBtn = await driver.findElements(By.css('.filters li'));
      await driver.actions().click(deleteBtn[2]).perform();
     })();
    

另外可以阅读: Clicking an element using javascript vs actions vs webdriver?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容