[英]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.
我已经尝试过使用xpath和CSS选择器,它们都正确地检索了正确的按钮。
Then when I call .click()
on the button, it does not follow the account. 然后,当我在按钮上调用
.click()
时,它不会跟随该帐户。
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 这是元素的图片
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: 首先,由于您使用的是
PhantomJS
,因此假装自己不是PhantomJS
可能会解决此问题,请参阅此文章,并提供有效的Python / Selenium示例:
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" : 您也可以尝试移动到元素,然后通过“ ActionChains”单击:
from selenium.webdriver import ActionChains
actions = ActionChains(driver)
actions.move_to_element(follow).click().perform()
Or, you can click "via JavaScript" : 或者,您可以点击“通过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 . 并且不要忘记遵守法律,遵守使用条款,成为一名良好的网络爬虫公民 。
通过从PhantomJS驱动程序切换到chromedriver,我可以使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.