我希望使用Selenium和PhantomJS webdriver自动下载我的Google外卖数据。我在Selenium IDE Firefox附加组件中开发了这些操作,并使用npm包selenium-html-js-converter
将测试用例的HTML输出转换为JS。我的选择器在Mozilla Firefox 45.0.2中完美运行,但是如果使用转换后的版本则会失败并显示以下消息:
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).
这是我要从此外卖中排除某些Google应用的部分。我手工构建所有选择器,因为我不想依赖元素的类和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中的用户代理字符串。
我的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@gmail.com")';
browser.elementById("Email").clear();
browser.elementById("Email").sendKeys("email@gmail.com");
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 + '/Exception@' + 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)的最新稳定版本。
这里出了什么问题?
答案 0 :(得分:0)
您需要引用每个属性值以生成XPath valide:
browser.elementByXPath("//tbody[@data-id='drive']/tr/td/div/").click();