Annie Shlepak
Annie Shlepak

Reputation: 103

Selenium with Python on ssh server: InvalidElementStateException & ElementNotVisibleException

I'm trying to log in into Quora website. On my local machine it runs perfectly.

But on SSH server (droplet on DigitalOcean) - no, I'm getting InvalidElementStateException

I've tried to focus on the element by send_keys(Keys.NULL), and got ElementNotVisibleException

Here is the code:

driver.get("https://www.quora.com/")
print("Logging...")

# gets email and password from json
with open('config.json') as f:
    login_data = json.load(f)
email = login_data['email']
password = login_data['pass']
time.sleep(3)
email_field_xpath = "//div[@class='form_column']/input[@name='email']"
password_field_xpath = "//div[@class='form_column']/input[@name='password']"

# webdriver's going to wait max 10 seconds for email's field, password field, login button to display
email_field_element = WebDriverWait(driver, 10).until(
    lambda driver: driver.find_element_by_xpath(email_field_xpath))
password_field_element = WebDriverWait(driver, 10).until(
    lambda driver: driver.find_element_by_xpath(password_field_xpath))

email_field_element.send_keys(Keys.NULL)
email_field_element.clear()
email_field_element.send_keys(email)
password_field_element.send_keys(Keys.NULL)
password_field_element.clear()
password_field_element.send_keys(password)
login_button_xpath = "//input[@value='Login']"
# wait till element is clickable
login_button_element = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.XPATH, login_button_xpath)))
login_button_element.click()
print("Logged In.")

Upvotes: 0

Views: 501

Answers (1)

Chanda Korat
Chanda Korat

Reputation: 2561

It may possible that screen resolution you are using at your local machine and on SSH server are different. May be the size of opened browser windows are different at both at local and on SSH server.

Try to change your local settings as SSH server's than you can figure out what is actual reason or just try the possible solutions given below.

Possible solutions by focusing on element first. Here, your element can be email_field_element or password_field_element:

1) First move to that element using ActionChains:

from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element(element)
actions.perform()

OR

2) You can also use scroll up to that element. Like:

driver.execute_script("arguments[0].scrollIntoView(true);", element)

OR

3)Input data using java script.

driver.execute_script("document.getElementById('elementID').setAttribute('value', 'new value for element')" # you can use xpath as well

Hope it may help you.

Upvotes: 0

Related Questions