var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
driver.get('https://www.tumblr.com/login');
driver.findElement(webdriver.By.id('signup_email')).sendKeys('my_username');
driver.findElement(webdriver.By.id('signup_password')).sendKeys('my_password');
driver.findElement(webdriver.By.id('signup_forms_submit')).click();
driver.wait(function(){
driver.get('https://www.tumblr.com/search/love+gifs');
},5000);
driver.wait(function(){
driver.findElement(webdriver.By.id("post_control like")).click();
},1000);
driver.wait(function(){
driver.findElement(webdriver.By.id("follow-text")).click();
},1000);
driver.quit();
<div class="post-info-post-tumblelog">
<div class="post-info-tumblelog">
<a class="follow_link worded-follow-button show-unfollow" data-subview="follow" href="/follow/pleasingpics" style="width: 41px;">
<div class="follow-text" title="Follow">Follow</div>
<div class="unfollow-text" title="Unfollow">Unfollow</div>
</a>
</div>
</div>
我有一个Javascript代码,可以使用Selenium WebDriver跟踪Tumblr上的一些帐户。
在HTML代码中,您可以在名为“follow-text”的类中看到一个跟随按钮。但我的代码不起作用,我也想知道按钮实际上不是一个按钮,但他们已经使它成为一个可点击的div类。我对HTML / CSS并不熟悉,而且我是Javascript和Selenium Webdriver的新手。我的代码出了什么问题?此外,我必须单击同一页面上不同帖子上的按钮。我正在考虑创建一个循环,但我甚至无法点击其中一个。任何帮助将不胜感激。
答案 0 :(得分:1)
driver.wait(function(){
var followButton = driver.findElement(webdriver.By.xpath("/html/body/div[4]/div/div/div[2]/div[2]/div[1]/div[2]/div/article[5]/header/div/div/div/a/div[1][@class='follow-text']"));
followButton.click();
},1000);
这段代码解决了我的问题。我猜firebug的xpath选择器给了我错误的答案。我改变了很多,最后它起作用了。谢谢大家的帮助。
答案 1 :(得分:0)
您需要单击父元素。不是在div上,而是在<a>
- 作为div的父。