简体   繁体   中英

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> 

I have a Javascript code for following some accounts on Tumblr using Selenium WebDriver.

In the HTML code, there is a follow button as you can see in a class named "follow-text". But my code doesn't work, also I'm wondering that follow button is not actually a button but they've made it a clickable div class. I'm not so familiar with HTML/CSS and I'm new with Javascript and Selenium Webdriver. What's wrong with my code? Also I have to click follow buttons on different posts on the same page. I'm thinking of creating a loop but I can't even click one of them. Any help will be appreciated.

 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); 

This code solved my problem. I guess firebug's xpath selector gave the wrong answer to me. I changed it a lot and finally it worked. Thank you all for your help.

You need to click on the parent element. Not on the div, but to the <a> -- as parent of div.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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