joe
joe

Reputation: 1723

Why isn't python's selenium button.click() working on a twitter follow button?

So I know I'm locating the correct element. I've tried it with xpath and css selectors, both are correctly retrieving the correct button.

Then when I call .click() on the button, it does not follow the account.

button = self.driver.find_element(By.CSS_SELECTOR, ".user-actions-follow-button.js-follow-btn.follow-button.btn")
print(button.text)
button.click()
print('should have followed')

Does anyone know why it's behaving like this?

Edit: Here's the whole class code:

class Scraper:
    def __init__(self):
        self.driver = webdriver.PhantomJS(executable_path='phantomjs')
        self.loggedIn = False;


    def login(self, url, username, password):
        self.driver.get(url)

        try:
            usernameTextField = self.driver.find_element_by_css_selector(".js-username-field.email-input.js-initial-focus")

            passwordTextField = self.driver.find_element_by_css_selector('.js-password-field')

            usernameTextField.send_keys(username)
            passwordTextField.send_keys(password + '\n')
        except:
            print('Already logged in')

        try:
            WebDriverWait(self.driver, 10).until(
                    EC.presence_of_element_located((By.ID, 'dashboard-profile-prompt'))
            )

        except:
            self.loggedIn = False

        finally:
            print('succesfully logged in')
            self.loggedIn = True

    def followByUrl(self, url):
        if self.loggedIn:
            self.driver.get(url)

            actions = ActionChains(self.driver)
            wait = WebDriverWait(self.driver, 10)
            follow = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".user-actions-follow-button.js-follow-btn.follow-button.btn")))


            print(follow.text)

            actions.move_to_element(follow).click().perform()

            print('should have followed')

And here's a picture of the element

enter image description here

Upvotes: 0

Views: 1069

Answers (2)

joe
joe

Reputation: 1723

I got it to work by switching from the PhantomJS driver to chromedriver.

Upvotes: 0

alecxe
alecxe

Reputation: 474191

First of all, since you are using PhantomJS, pretending not to be PhantomJS might solve the problem, see this post with a working Python/Selenium sample:

And, make sure you are clicking the same button you are checking to be clicked. There can be multiple "Follow" buttons on a page.

Also, add an Explicit Wait to wait for the button to become clickable:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)

follow = wait.until(element_to_be_clickable((By.CSS_SELECTOR, "button.follow-button")))
follow.click()

You may also try moving to the element and then clicking via "ActionChains":

from selenium.webdriver import ActionChains

actions = ActionChains(driver)
actions.move_to_element(follow).click().perform()

Or, you can click "via JavaScript":

driver.execute_script("arguments[0].click();", follow)

And don't forget to stay on the legal side, follow the Terms of Use, be a good web-scraping citizen.

Upvotes: 1

Related Questions