单击按钮但程序崩溃了selenium webdriver

时间:2015-11-30 18:42:35

标签: javascript selenium button selenium-webdriver



<div class="post-info-tumblelogs">
<div class="post-info-post-tumblelog">
<div class="post-info-tumblelog">
<a class="follow_link worded-follow-button" data-subview="follow" href="/follow/iglovequotes">
<div class="follow-text" title="Follow">Follow</div>
<div class="unfollow-text" title="Unfollow">Unfollow</div>
</a>
</div>
</div>
</div>
&#13;
&#13;
&#13;

&#13;
&#13;
var webdriver = require('selenium-webdriver');

var driver = new webdriver.Builder().
   withCapabilities(webdriver.Capabilities.chrome()).
   build();
driver.manage().window().maximize();


driver.get('https://www.tumblr.com/login');
driver.findElement(webdriver.By.id('signup_email')).sendKeys('username');
driver.findElement(webdriver.By.id('signup_password')).sendKeys('password');
driver.findElement(webdriver.By.id('signup_forms_submit')).click();

driver.get('https://www.tumblr.com/search/tv+series');


/*  THIS PART IS FOR FOLLOW
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[3]/header/div/div/div/a/div[1][@class='follow-text']"));
  followButton.click();
},3000);
*/

// THIS PART IS FOR LOVE BUTTON
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[2]/div/div[2]/div/div[@class='post_control like']"));
  followButton.click();
},3000);



driver.quit();
&#13;
&#13;
&#13;

我使用selenium webdriver和javascript。在这段代码中,我试图点击tumblr页面中的关注按钮和爱情按钮,但是在点击按钮后,它会出错并且不会执行下一个操作,它会崩溃。关注和爱情按钮的错误是这样的:

&#13;
&#13;
goog.global.setTimeout(function() { throw exception; }, 0);
                                      ^
NoSuchElementError: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[4]/div/div/div[2]/div[2]/div[1]/div[2]/div/article[2]/div/div[2]/div/div[@class='post_control like']"}
&#13;
&#13;
&#13;

&#13;
&#13;
UnknownError: unknown error: Element is not clickable at point (934, 590). Other element would receive the click: <div class="unfollow-text" title="Unfollow">...</div>
&#13;
&#13;
&#13;

第一个错误是爱情按钮,第二个错误是关注按钮。我该怎么做才能解决这个问题?在两个操作中,它都会成功并单击按钮但错误会阻止我在同一代码中执行另一个操作。我在等你的帮忙。

2 个答案:

答案 0 :(得分:1)

您正在使用绝对xpath

尝试这样的事情: -

driver.findElement(By.xpath("//div[@title='Unfollow']"));

如果您在此处发布了完整的HTML代码或网页链接,那么我们可以为您提供更准确的帮助。

希望它能帮到你:)

答案 1 :(得分:1)

我认为tumblr也进行了a / b测试。用户名和密码可以通过一步或两步询问,因此webdriver可能无法在页面上找到正确的元素。