Home > Enterprise >  How to close the Flipkart login window by Selenium WebDriver?
How to close the Flipkart login window by Selenium WebDriver?

Time:01-11

When I run the below script the website is opened but the popup window is also opened. How do I close this popup window so the script can continue?

from selenium import webdriver

driver = webdriver.Chrome("C://browserdrivers//chromedriver.exe")  
driver.maximize_window()
driver.get('https://www.flipkart.com/')
driver.find_element_by_xpath("/html/body/div[2]/div/div/button").click()

Screenshot:

Take a look on screenshot also.

CodePudding user response:

This is a little bit trickee since all attributes of that X button element and it parent elements seems to be dynamic. Also that X text is not x or X letter.
So, I located it saying: "give me a button element containing some text but not containing 'OTP' text". This give an unique locator and the folllowing code works:

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(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)

url = "https://www.flipkart.com/"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()][not(contains(.,'OTP'))]"))).click()

CodePudding user response:

Another alternative solution would be issuing a random positioned click to dismiss the login window. For an example

driver.execute_script('el = document.elementFromPoint(47, 457); el.click();')

CodePudding user response:

The element opens in a Modal Window


To click() on the desired element you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategy:

  • Using XPATH:

    driver.get('https://www.flipkart.com/')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[text()='✕']"))).click()
    
  • Note: You have to add the following imports :

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