繁体   English   中英

无法单击到div按钮Selenium Webdriver

[英]can't click to a div button selenium webdriver

 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代码中,有一个跟随按钮,您可以在名为“跟随文本”的类中看到。 但是我的代码不起作用,我也想知道跟随按钮实际上不是按钮,但是他们已经将其设为可点击的div类。 我对HTML / CSS不太熟悉,对Javascript和Selenium Webdriver还是很陌生。 我的代码有什么问题? 另外,我还必须单击同一页面上不同帖子上的关注按钮。 我正在考虑创建一个循环,但是我什至不能单击其中之一。 任何帮助将不胜感激。

 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选择器给了我错误的答案。 我做了很多修改,终于奏效了。 谢谢大家的帮助。

您需要单击父元素。 不是在div上,而是在<a> -作为div的父级。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM