Disclaimer - I'm not even remotely fluent with HTML, so please bear with me...
I'm automating a weekly data download process and one website (ChargePoint) displays some sort of pop-up if there is a charging sessions active at the moment. I need to close that pop-up in order to get to the main window and activate the data download. But I cannot figure out how to close the pop-up because trying to locate any element in the pop-up window fails.
I cannot figure out how to obtain the HTML to paste here, so here's a screenshot of the offending pop-up window and the corresponding HTML:
I've tried locating the "Close" button using XPATH and CLASSS_NAME; both methods fail:
close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,
"//*[contains(text(), 'Close')]")))
and
close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME,
"sc-iHmpnF knKEkw modal-close")))
What am I doing wrong? And if it helps, how do I get the actual HTML code to paste into this post?
Disclaimer - I'm not even remotely fluent with HTML, so please bear with me...
I'm automating a weekly data download process and one website (ChargePoint) displays some sort of pop-up if there is a charging sessions active at the moment. I need to close that pop-up in order to get to the main window and activate the data download. But I cannot figure out how to close the pop-up because trying to locate any element in the pop-up window fails.
I cannot figure out how to obtain the HTML to paste here, so here's a screenshot of the offending pop-up window and the corresponding HTML:
I've tried locating the "Close" button using XPATH and CLASSS_NAME; both methods fail:
close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,
"//*[contains(text(), 'Close')]")))
and
close_btn = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME,
"sc-iHmpnF knKEkw modal-close")))
What am I doing wrong? And if it helps, how do I get the actual HTML code to paste into this post?
Share Improve this question asked Feb 3 at 21:03 BillRBillR 431 silver badge7 bronze badges 2 |1 Answer
Reset to default 1Using a CSS selector you could try to find the button like this:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdrivermon.by import By
from selenium.webdriver import Chrome
from selenium.webdrivermon.action_chains import ActionChains
from seleniummon.exceptions import TimeoutException
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
URL = "your url goes here"
def click(driver: WebDriver, e: WebElement) -> None:
ActionChains(driver) \
.move_to_element(e) \
.click() \
.perform()
if __name__ == "__main__":
with Chrome() as driver:
driver.get(URL)
wait = WebDriverWait(driver, 2.5)
ec = EC.element_to_be_clickable
sel = By.CSS_SELECTOR, "button[data-testid=modal-close]"
try:
button = wait.until(ec(sel))
click(driver, button)
except TimeoutException:
pass # the button may not exist so ignore the timeout
By.CLASS_NAME
does not allow multiple class names. See stackoverflow/q/44759907/494134 – John Gordon Commented Feb 3 at 21:27