要自动从某官方网站上自动下载某文件,结果因为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:
Javascript:
下次查找相关解决方法时,可以注意下对应解决办法是否支持当前所使用的语言;而且发现下面语句的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类名
遗留下的问题:
inline mode和external js mode有什么区别吗?
-
关于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?