admin管理员组文章数量:1305173
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
本文标签: pythonCannot access elements in a popup window to close itStack Overflow
版权声明:本文标题:python - Cannot access elements in a pop-up window to close it - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741797759a2398036.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
By.CLASS_NAME
does not allow multiple class names. See stackoverflow/q/44759907/494134 – John Gordon Commented Feb 3 at 21:27