所以我知道我正在找到正確的元素。我用xpath和css選擇器試過,都正確地檢索正確的按鈕。爲什麼python的selenium button.click()工作在twitter按鈕上?
然後當我打電話.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')
有沒有人知道它爲什麼會這樣?
編輯: 這裏是整個類代碼:
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')
而這裏的元素
你有什麼錯誤?一個'Element not visible'錯誤提示該元素尚未加載,並且在動態加載的頁面上很常見。 – theeastcoastwest
@Frank沒有得到任何錯誤。它也正確地打印出按鈕。文字 – joe
通常當我遇到這種問題時,它意味着錯誤的元素正在接收該動作。 – theeastcoastwest