user9886249
user9886249

Reputation:

Selenium WebElements difficulty

I am currently working on a program that permit to connect directly to some social network. Here is the code:

browser = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
browser.get("https://www.instagram.com") 

username = "Richard"

browser.get('https://www.instagram.com')
input_username = browser.find_elements_by_xpath(
    "//input[@name='username']"
)

action=ActionChains(browser)
action.move_to_element(input_username)
action.click()
action.send_keys(username)
action.perform()

And I got a problem because I got this error:

AttributeError: move_to requires a WebElement

What can I do to resolve that?

Upvotes: 0

Views: 664

Answers (1)

Andersson
Andersson

Reputation: 52665

input_username is not a WebElement, but a list as you've used find_elements_by_xpath() instead of find_element_by_xpath().

Try either

input_username = browser.find_element_by_xpath("//input[@name='username']")
action.move_to_element(input_username)

or

input_username = browser.find_elements_by_xpath("//input[@name='username']")
action.move_to_element(input_username[0])

Note that Authentication form generated dynamically by JavaScript, so to be able to handle input fields, you need to wait for JavaScript to be executed:

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

input_username = wait(browser, 10).until(EC.element_to_be_clickable((By.NAME, "username")))

Upvotes: 2

Related Questions