Firefox -> PhantomJs Login to website not working

2020-06-30 00:22发布

问题:

For some reason when I don't run headless I am able to log into http://www.neighborhoodscout.com. However when I run headless PhantomJS I am unable to login and it can't find an element it returns on success of a login.

Phantom JS version 1.9.8

driver = webdriver.PhantomJS()
driver.set_window_size(1124, 850)  
driver.get(url)
time.sleep(2)
driver.find_element_by_id("login").click()
time.sleep(2)
driver.find_element_by_id("user_email").send_keys(username)
driver.find_element_by_id("user_password").send_keys(password)
time.sleep(4)
driver.find_element_by_id("login-button").click()

Why does it work for non headless vs not work for phantomjs??

There is no traceback error, it just is unable to login for some reason?

I am looking for a specific element by doing:

ele_found=EC.presence_of_element_located(driver.find_element_by_id('flash_notice')) is not None

回答1:

Below code works well for me!

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

url = 'http://www.neighborhoodscout.com/'
username = 'abc@bcas.fulba.com'#change here
password = '12345'#change here
driver = webdriver.PhantomJS(r"C:\phantomjs.exe")
driver.set_window_size(1124, 850)  
driver.get(url)
time.sleep(2)
login = driver.find_element_by_xpath("//a[@ id='login']")
login.click()
time.sleep(5)
usr = driver.find_element_by_xpath("//input [@id='user_email' and @class='input-text']")
passw = driver.find_element_by_xpath("//input [@id='user_password' and @class='input-text' ]")
logbtn = driver.find_element_by_xpath("//input [@id='login-button' and  @class='btn']")
usr.send_keys(username)
passw.send_keys(password)
logbtn.click()

time.sleep(4)

if '/users/sign_out' in driver.page_source:
    print "Logged In"
driver.close()