我试图滚动到页面上的链接元素并单击它。 以下解决方案适用于Chrome和IE,但不适用于Firefox。我怎么能解决这个问题?还有其他方法吗?
function clickByLinkTextScroll(text){
driver.findElement(By.linkText(text)).getLocation().then(function (location) {
driver.executeScript("window.scroll(0," + location.y + ");");
driver.sleep(300);
driver.findElement(By.linkText(text)).click();
});
};
当我针对Firefox运行时,我得到以下输出:
UnknownCommandError: GET /session/ad17a19d-782b-4706-b1d7-b56f0139b252/element/5c5e0c3d-1861-432c-b402-b6a010c2f268/location did not match a known command
at WebDriverError (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\error.js:27:5)
at UnknownCommandError (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\error.js:296:5)
at Object.throwDecodedError (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\error.js:477:11)
at parseHttpResponse (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\http.js:388:15)
at doSend.then.response (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\http.js:330:11)
at process._tickCallback (internal/process/next_tick.js:103:7)
From: Task: WebElement.getLocation()
at Driver.schedule (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\webdriver.js:414:17)
at WebElementPromise.schedule_ (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\webdriver.js:1823:25)
at WebElementPromise.getLocation (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\webdriver.js:2112:17)
at clickByLinkTextScroll (innovationCentralTests.js:694:43)
at Context.<anonymous> (innovationCentralTests.js:445:9)
at C:\ProgramData\npm\node_modules\selenium-webdriver\testing\index.js:153:19
at new ManagedPromise (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:1017:7)
at controlFlowExecute (C:\ProgramData\npm\node_modules\selenium-webdriver\testing\index.js:138:14)
at TaskQueue.execute_ (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:2731:14)
at TaskQueue.executeNext_ (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:2714:21)
at asyncRun (C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:2631:25)
at C:\ProgramData\npm\node_modules\selenium-webdriver\lib\promise.js:639:7
答案 0 :(得分:8)
正如Sudharsan Selvaraj建议的那样,我使用了以下内容并且很好地完成了这项工作:
function clickByLinkTextScroll(text) {
element = driver.findElement(By.linkText(text));
driver.executeScript("arguments[0].scrollIntoView()", element);
driver.sleep(300);
element.click();
};