1

So I'm trying to scrape some information from a website and can't get through a pop-up window. I've tried using short and full Xpath of the X button but it doesn't close.

here is my code

# import 
from selenium import webdriver

from selenium.common.exceptions import TimeoutException

from selenium.webdriver.support.ui import WebDriverWait 

from selenium.webdriver.support import expected_conditions as EC 

driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe') 

driver.get('https://ai.fmcsa.dot.gov/SMS')

driver.find_Element_By_xpath('//*[@id="simplemodal-container"]/a').click();

The code does open the website but doesn't close the pop-up. What might be the issue?

1
  • I'm guessing that the popup doesn't appear straight away so it doesn't find it. Make it sleep for a few seconds and see if it works. Commented Sep 3, 2021 at 17:45

2 Answers 2

1

You automation script needs an explicit waits, and the below xpath :-

//a[@title='Close']

Code : -

driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe') 
driver.maximize_window()
#driver.implicitly_wait(50)
driver.get("https://ai.fmcsa.dot.gov/SMS")
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[@title='Close']"))).click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Sign up to request clarification or add additional context in comments.

Comments

0

If you want to use your code as is:

# import 
from selenium import webdriver

from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 

driver = webdriver.Chrome(executable_path = 'mypath/chromedriver.exe') 
driver.get('https://ai.fmcsa.dot.gov/SMS')
driver.find_Element_By_xpath('/html[1]/body[1]/div[7]/a[1]').click();

This worked for me and closed the pop up window.

1 Comment

This one is not working for me, I'm not sure why but the one offered by cruisepandey below does.

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.