pythonseleniumweb-scrapingchrome-web-driver

I can't find an element in Selenium.(Python/Google)


This is the link https://webmail1.hostinger.com/

I want to locate the 2 input boxes and input my email and password, but i can't acess them. I can easily find them by inspecting the page but i can't find them by their xpath, selector, id etc.

I considered that they may be inside a frame and i acessed the frames within the page to no avail.

I also tried to insert a wait period in case there is something to load, but that didn't solve the problem. Here is how the related section of my code currenly stands:

driver.execute_script("window.open('https://webmail1.hostinger.com/');)
sleep(10)

iframes = driver.find_elements_by_tag_name("iframe")
driver.switch_to.frame(iframes[0])

element = WebDriverWait(driver, 30).until(
       EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div[1]/form/table/tbody/tr[1]/td[2]/input"))
    )

element.send_keys("test")

Thank you for your help


Solution

  • IMPORTANT: Check this answer if you can't open an element AFTER opening a new Tab. This was my problem*

    72 tries and about 5 hours later i finally found the answer. None of the answers above helped me, perhaps because i wasn't clear enough.

    Basically, the first part of my code opened a window, inserted password, email etc and everything went fine. Then, i opened a new tab with>(which someone here claimed to be Javascript. It may be, but it is definitely a command in python too.)

    driver.execute_script("window.open('https://webmail1.hostinger.com/');)
    

    I tried finding elements from this new tab that had been opened, but you need to tell the webdriver to switch to the new tab. I thought only opening it was enough for the webdriver to automatically swith to it. Basically, i was searching for elements of tab 2 in tab 1's html, and that is why i wasn't finding them. Here is my code now:

    #opens new tab
    driver.execute_script("window.open('https://webmail1.hostinger.com/');")
    #switches to the second tab 
    driver.switch_to.window(driver.window_handles[1])
    driver.get(tab_url)
    
    email= driver.find_element_by_name('_user')
    email.send_keys("my_email")
    
    password = driver.find_element_by_id("rcmloginpwd")
    password.send_keys("my_password")
    

    It works like a charm. This is the link were i found about this. Go there for more information