Search code examples
pythonselenium-webdriverautomation

Selenium python send_keys doesnt work (though textfield is found and clicked)


def login(self):
    implicitly_wait(30)
    login_button = self.find_element(By.XPATH, "//*[contains(@href, '/m-einloggen.html?targetUrl=%2Fs-anzeige%3Agesuche%2Fheimarbeit%2Fk0')]")
    login_button.click() 
    login_button.click() 
    self.implicitly_wait(30)
    email_button = self.find_element(By.ID, "email")
    email_button.clear() 
    self.implicitly_wait(30)
    email_button.click()
    email_button.send_keys(const.mail)

My error message:

I'm trying to write an eBay automation with python. But it doesn't seem to work to pass keys to the field where you enter your mail to login. It seems to be working that the button with the email text field is clicked, due to the fact that the cursor is blinking in it. But the keys aren't passed. I already tried clearing the field and implicit wait, in case my internet connection is too slow. Also I'm using the latest chrome version 119.0.6045.160.... so I really don't know what could cause the error. The error output is in the png.


Solution

  • Root cause of the issue: Your below XPath expression is not accurate, it is locating 2 web elements.

    //*[contains(@href, '/m-einloggen.html?targetUrl=%2Fs-anzeige%3Agesuche%2Fheimarbeit%2Fk0')]
    

    Solution: Use the below XPath expression to locate log-in element which is more simpler and easy to read:

    //span[text()='Alle akzeptieren']
    

    Refactored working code below with in-line explanation:

    import time
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome()
    driver.maximize_window()
    driver.get("https://www.kleinanzeigen.de/s-anzeige:gesuche/heimarbeit/k0")
    
    # create wait object which wait for 15s based on the condition
    wait = WebDriverWait(driver, 15)
    
    # Below line will click on Alle akzeptieren button
    wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Alle akzeptieren']"))).click()
    
    # Below line will click on Einloggen button
    wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Einloggen']"))).click()
    
    # Below line will send email into text box
    wait.until(EC.element_to_be_clickable((By.ID, "email"))).send_keys("[email protected]")
    time.sleep(20)
    

    Result:

    enter image description here

    UPDATE: Refer the below screenshot. You will notice your XPath expression finds 2 results. Both the elements are in the screenshot highlighted in yellow.

    enter image description here