Webdriver Javascript如何在模态中单击下载按钮

时间:2015-02-20 14:00:42

标签: node.js webdriver selenium-chromedriver browser-automation

我正在尝试构建一个使用chromedriver和selenium-webdriver运行的node.js脚本:登录到网站,导航到下载部分并单击下载按钮。

网站是推特,数据是csv文件中的广告系列数据。

我已经将它们整合在一起,所以我的代码不会很完美或很棒。

我基本上处于最后一点,我需要点击一个downloadButton,其中包含以下' Inspect Element'数据:

<button id="downloadButton" class="btn btn-primary">Download</button>

我无法在页面源中找到此html,只能通过&#39; Inspect Element&#39; (使用铬)

main.js:

var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();

username = "username"
password = "password"

driver.manage().timeouts().implicitlyWait(30000);

// Navigate to Twitter ads login page //
driver.get('https://twitter.com/login?redirect_after_login=https%3A%2F%2Fads.twitter.com%2F');

// Enters a username into the username box //
driver.findElement(webdriver.By.xpath("//form[@class=\"t1-form clearfix signin js-signin\"]//input[@name=\"session[username_or_email]\"]")).sendKeys(username);

// Enters a password into a password box //
driver.findElement(webdriver.By.xpath("//form[@class=\"t1-form clearfix signin js-signin\"]//input[@name=\"session[password]\"]")).sendKeys(password);

//Clicks the sign in button
driver.findElement(webdriver.By.xpath("//form[@class=\"t1-form clearfix signin js-signin\"]//button[@class=\"submit btn primary-btn\"]")).click()

//Clicks the campaign of choice //
driver.findElement(webdriver.By.xpath("//form[@id=\"account-selector-form\"]/div[2]/ul/li[2]/a/img")).click();

// Selects the calendar // 
driver.findElement(webdriver.By.xpath("//div//*[@id=\"dashboardBox\"]/form/div/div/span[2]")).click();

//Click on year of data //
driver.findElement(webdriver.By.xpath("//div[7]/div[4]/ul/li[6]")).click();

//Clicks the export button // 
driver.findElement(webdriver.By.xpath("//div//*[@id=\"dashboard-header\"]/div/div/div/button/span")).click();

//This bit doesnt work: //
driver.findElement(webdriver.By.id("downloadButton")).click()

packages.json:

{
  "name": "webdriver-recipes",
  "private": true,
  "devDependencies": {
    "selenium-webdriver": "~2.42.1"
  }
}

感谢您的帮助。

0 个答案:

没有答案