【问题标题】:can't click to a div button selenium webdriver无法单击 div 按钮 selenium webdriver
【发布时间】:2016-03-02 21:52:39
【问题描述】:

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 的新手。我的代码有什么问题?此外,我必须单击同一页面上不同帖子上的关注按钮。我正在考虑创建一个循环,但我什至无法单击其中一个。任何帮助将不胜感激。

【问题讨论】:

  • 这里不是最重要的,但很确定这会违反 Tumblr 的 T's & C's: "... 或超载服务,或通过脚本使用服务这种方式会干扰服务或给服务造成不适当的负担。” - 每秒 1 次关注可能被视为垃圾邮件/负担。
  • 我不知道条款和条件,但这是我实习计划的一部分,这就是我这样做的原因,因为这对我很重要。操作之间的时间可能会超过 1 秒,但无论如何,感谢您的评论。
  • 如果是实习,他们要求你这样做,要么故意违反规则,要么在不知情的情况下 - 这是一个不好的迹象。

标签: javascript node.js selenium selenium-webdriver


【解决方案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);

这段代码解决了我的问题。我猜萤火虫的 xpath 选择器给了我错误的答案。我改变了很多,终于奏效了。谢谢大家的帮助。

【讨论】:

    【解决方案2】:

    您需要单击父元素。不是在 div 上,而是在 &lt;a&gt; -- 作为 div 的父级。

    【讨论】:

    • 我尝试单击以
    • 由于 Selenium 的原因,它不适用于类名的编译。您只需要通过 xpath 或“follow_link”搜索
    • @ÇağatayBarın 你修复了 issiu 吗?
    • 不,我正在尝试不同的东西,例如 By.className 或 By.xpath 或 By.cssSelector 但它不起作用:(
    • @ÇağatayBarın by class "follow_link" 对你有用 =) 试试吧
    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-17
    • 2016-11-26
    • 2021-11-09
    • 2022-12-05
    • 1970-01-01
    相关资源
    最近更新 更多