0

I am very new to Python, still trying to figure everything out.

While trying my hand at web-scraping I got into a problem:

The website requires a sign in and a pop-up shows up as soon as search results page shows up and sometimes while scrolling and looking at results. So I need to press escape and then hover over(or I can click it) the "Sign In" drop-down menu and click: "Sign in" in the drop down menu.

But right now, from an education point of view I wanted to move the mouse to the element using its XPath.

I did the following and it doesn't work.

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

chrome_path = r"C:\Users\------\Desktop\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("URL of the webnsite I am scraping")  #sorry had to remove the link :(
driver.maximize_window()


#code below this is not working
action = webdriver.ActionChains(driver)
action.move_to_element((By.XPATH, '//*[@id="user_sign_in"]'))



 #Tried the ones below and they didn't work either
 #driver.move_to_element(By.XPATH, '//*[@id="user_sign_in"]')
 #login_menu = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="user_sign_in"]')))

1 Answer 1

1

You forgot to apply your actions with perform():

action.move_to_element((By.XPATH, '//*[@id="user_sign_in"]')).perform()

And, you can use the "by id" locator instead - using XPath for such simple location problem is an over-complication:

elm = driver.find_element_by_id("user_sign_in")
action.move_to_element(elm).perform()

If this would still no work, here are the possible reasons:

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, both of the above worked. The second one is what I am using now. Now to figure out how to replace a placeholder text when I get the sign in clicked. It's in a pop up and I can find the xpath and id, however I get the following error: AttributeError: 'list' object has no attribute 'send_keys'
@Sid you are probably using the find_elements_* (which returns a list of elements) instead of find_element_* (which returns a single element).
Thanks, I was making that exact mistake. Now that I changed it got a whole new error: Message: unknown error: cannot focus element The code I used inputElement = driver.find_element_by_xpath('//*[@id="lfm"]/div[1]/div[2]') #driver.find_elements_by_xpath("//*[contains(text(), 'Enter your Mobile Number')]") #driver.find_element_by_id("mobile") inputElement.send_keys('Phone Number') inputElement.submit().
Element I am basing it on: The element I am basing this on: <input tabindex="1" style="padding-left:119px!important;width:318px!important;background-color:#fff;outline: none;box-sizing: inherit;" id="email" name="mobile" placeholder="Enter your Mobile Number" class="un_s un1_s" value="" onblur="remove_border();" type="text" maxlength="20">. Thanks for all the help and sorry to take up so much of your time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.