我想使用PhantomJS webdriver使用Selenium自動下載我的Google外帶數據。我開發了Selenium IDE Firefox插件中的操作,並使用npm包selenium-html-js-converter
將測試用例的HTML輸出轉換爲JS。我的選擇是在Mozilla Firefox 45.0.2完美的工作,但如果使用轉換版本,它失敗,出現以下消息:硒與幻影JS失敗,但在火狐工作
Error: Failure in Selenium command "click("//tbody[@data-id=drive]/tr/td/div/", "")": [elementByXPath("//tbody[@data-id=drive]/tr/td/div/")] Error response status: 32, , InvalidSelector - Argument was an invalid selector (e.g. XPath/CSS).
這是我想從這個外賣排除某些谷歌應用程序的一部分。我手動構建所有選擇器,因爲我不想依賴元素的類和ID,因爲這些元素是由某個構建工具自動生成的,並且可以隨時更改。
I read in this post引號可能是一個問題,但評估仍然失敗,沒有引號。我還嘗試將我的Firefox用戶代理更改爲PhantomJS正在使用的用戶代理(Mozilla/5.0 (Unknown; Linux x86_64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1
),以確保網站輸出相同,但仍能正常工作。但是我不知道如何更改Selenium中的用戶代理字符串。
我的硒核心代碼如下所示(我省略了自動生成的輔助功能,以保持它的整潔):
"use strict";
/* jslint node: true */
var assert = require('assert');
var browser, element, currentCommand = '',
options = {
timeout: 30000,
retries: 0,
screenshotFolder: 'screenshots/test_minimal_takeout_mobile_html',
baseUrl: 'https://accounts.google.com/'
};
module.exports = function testMinimalTakeoutMobileHtml(_browser, _options) {
browser = _browser;
var acceptNextAlert = true;
getRuntimeOptions(_options);
try {
currentCommand = 'open("/ServiceLogin?passive=1209600&continue=https%3A%2F%2Faccounts.google.com%2FManageAccount#identifier", "")';
browser.get(addBaseUrl("/ServiceLogin?passive=1209600&continue=https%3A%2F%2Faccounts.google.com%2FManageAccount#identifier"));
currentCommand = 'type("id=Email", "[email protected]")';
browser.elementById("Email").clear();
browser.elementById("Email").sendKeys("[email protected]");
currentCommand = 'click("id=next", "")';
browser.elementById("next").click();
currentCommand = 'uncheck("id=PersistentCookie", "")';
if (browser.elementById("PersistentCookie").isSelected()) {
browser.elementById("PersistentCookie").click();
};
currentCommand = 'type("id=Passwd", "password")';
browser.elementById("Passwd").clear();
browser.elementById("Passwd").sendKeys("password");
currentCommand = 'clickAndWait("id=signIn", "")';
doAndWait(function() {
browser.elementById("signIn").click();
});
currentCommand = 'open("https://takeout.google.com/settings/takeout", "")';
browser.get(addBaseUrl("https://takeout.google.com/settings/takeout"));
currentCommand = 'click("//tbody[@data-id=\'drive\']/tr/td/div/", "")';
//<<< It fails here >>>
browser.elementByXPath("//tbody[@data-id=\'drive\']/tr/td/div/").click();
currentCommand = 'click("//tbody[@data-id=\'chat\']/tr/td/div/", "")';
browser.elementByXPath("//tbody[@data-id=\'chat\']/tr/td/div/").click();
currentCommand = 'click("//tbody[@data-id=\'gmail\']/tr/td/div/", "")';
browser.elementByXPath("//tbody[@data-id=\'gmail\']/tr/td/div/").click();
currentCommand = 'click("//div[@data-state=\'1\']/div[2]/div[2]/div", "")';
browser.elementByXPath("//div[@data-state=\'1\']/div[2]/div[2]/div").click();
currentCommand = 'mouseDown("//div[@data-param=\'destination\']/div[2]/div[@role=\'presentation\']/div[2]", "")';
browser.elementByXPath("//div[@data-param=\'destination\']/div[2]/div[@role=\'presentation\']/div[2]").mouseDown();
currentCommand = 'mouseUp("//div[@data-param=\'destination\']/div[2]/div[@role=\'presentation\']/div[2]", "")';
browser.elementByXPath("//div[@data-param=\'destination\']/div[2]/div[@role=\'presentation\']/div[2]").mouseUp();
currentCommand = 'click("//div[@data-param=\'destination\']/div[2]/div[3]/div[@data-value=\'DRIVE\']", "")';
browser.elementByXPath("//div[@data-param=\'destination\']/div[2]/div[3]/div[@data-value=\'DRIVE\']").click();
currentCommand = 'click("//div[@data-state=\'2\']/div[2]/div[2]/div", "")';
browser.elementByXPath("//div[@data-state=2]/div[2]/div[2]/div").click();
} catch (e) {
var failedScreenShot = options.screenshotFolder + '/[email protected]' + currentCommand.replace(/\(.+/, '') + '.png';
try {
createFolderPath(options.screenshotFolder);
browser.saveScreenshot(failedScreenShot);
} catch (e) {
e.message = 'Failure in Selenium command "' + currentCommand + '": ' + e.message + ' (Could not save screenshot after failure occured)';
throw e;
}
e.message = 'Failure in Selenium command "' + currentCommand + '": ' + e.message + ' (Screenshot was saved to ' + failedScreenShot + ')';
throw e;
}
};
這是採取在測試失敗時顯示我正確的頁面截圖。
我正在使用NodeJS(5.10.1),PhantomJS(2.1.1)和Selenium(2.53.1)的最新穩定版本。
這裏怎麼回事?
屬性值與其中自動生成的代碼和我第一次嘗試它未能報價。但是,我將編輯我的答案以顯示正確的XPath。 – Noir
XPath仍然不正確,您需要刪除報價前的轉義。 –