Unable to write any text in the input field even though this element seems to be in focus

Viewed 39

The following code is not writing any partial string in the From input field on the website even though this element seems to be an active element.

I spent lot of time trying to debug and make the code work but no success. Can anyone please provide some hint on what is wrong. Thanks.

from selenium import webdriver

from selenium.webdriver.common.by import By

import time

from selenium.webdriver.common.keys import Keys

from colorama import init, Fore

class BookingTest1():

    def __init__(self):

        pass

    def test1(self):

        baseUrl="https://www.goibibo.com/"

        driver=webdriver.Chrome()

        driver.maximize_window()

        #open airline site

        driver.get(baseUrl)

        driver.implicitly_wait(3)

           

        # Enter origin location.

        partialTextOrigin="New"



        #select flight tab            

        driver. find_element(By.XPATH,"//ul[@class='happy-nav']//li//a[@href='/flights/']").click()

        # select input box            

        textElement = driver.find_element(By.XPATH, "//input")

        # check if input box is active

        if textElement==driver.switch_to.active_element:

            print('element is in focus') 

            textElement.send_keys(partialTextOrigin)           

        else:

            print('element is not in focus')

            print("Focus Event Triggered")

            driver.execute_script("arguments[0].focus();", textElement)

            time.sleep(5)

            if textElement==driver.switch_to.active_element:

                print('finally element is in focus')

                print(partialTextOrigin)

                textElement.send_keys(partialTextOrigin)

                time.sleep(5)


#test the code

tst=BookingTest1()

tst.test1()
1 Answers

There are several issues here:

  1. First you need to click on p element in the From block and only after that when input appears there you can insert the text to it.
  2. You should use unique locators. (There more that 10 input elements on this page)
  3. Using WebDriverWait expected conditions explicit waits are much better than implicitly_wait in most cases.
  4. No need to set timeouts to too short values.
  5. No need to use driver.switch_to.active_element here.
    The following code works for me:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC


options = Options()
options.add_argument("start-maximized")


webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
url = "https://www.goibibo.com/"
flights_xpath = "//ul[@class='happy-nav']//li//a[@href='/flights/']"
from_xpath = "//div[./span[contains(.,'From')]]//p[contains(text(),'Enter city')]"
from_input_xpath = "//div[./span[contains(.,'From')]]//input"
partialTextOrigin = "New"

wait = WebDriverWait(driver, 10)
driver.get(url)
wait.until(EC.element_to_be_clickable((By.XPATH, flights_xpath))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, from_xpath))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, from_input_xpath))).send_keys(partialTextOrigin)

from_xpath and from_input_xpath XPath locators are a little complex.
I was not sure about the class names in that elements block if they are fixed so I based on the texts.
For example "//div[./span[contains(.,'From')]]//p[contains(text(),'Enter city')]" means:
Find such div that it has a direct span child so that span contains From text content.
From the div parent element above find inside it a p child that contains Enter city text.
Similarly to the above locator "//div[./span[contains(.,'From')]]//input" means: find parent div as described before, then find inside it an input child element.

The result of the code above is

enter image description here

Related