python-3.x selenium xpath css-selectors webdriverwait

python 3.x - ElementNotVisibleException: Mensaje: error no interactable del elemento al intentar hacer clic en un botón a través de Selenium y Python



python-3.x xpath (2)

Tengo una página con código fuente como el siguiente código. En él, después de realizar una acción, aparece "Deshacer" y un botón "Cerrar". Estoy tratando de hacer clic en el botón "Cerrar". He intentado los tres trozos de código a continuación, ninguno está funcionando. ¿Alguien puede indicar qué estoy haciendo mal o sugerir otra cosa para intentarlo?

fuente html:

<div class="_3Aslx7L3GVI4XM7PUyYKza action-bar"><div class="container"><i class="success-icon fontello-ok-circle"></i><div class="success-message">Your stuff is going to <span>place</span> is on its way.</div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--gray"> Undo</button></div><div class="gh69ID1m3_xtdTUQuwadU"><button class="c-button c-button--blue"> Close</button></div></div></div>

intentos de código:

#driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click() driver.find_element_by_css_selector(''.c-button.c-button--blue'').click() #driver.find_element_by_link_text(''Close'').click()

error:

--------------------------------------------------------------------------- ElementNotVisibleException Traceback (most recent call last) <ipython-input-15-6d570be770d7> in <module>() 1 #driver.find_element_by_id("gh69ID1m3_xtdTUQuwadU").click() ----> 2 driver.find_element_by_css_selector(''.c-button.c-button--blue'').click() 3 #driver.find_element_by_link_text(''Close'').click() ~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in click(self) 78 def click(self): 79 """Clicks the element.""" ---> 80 self._execute(Command.CLICK_ELEMENT) 81 82 def submit(self): ~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webelement.py in _execute(self, command, params) 626 params = {} 627 params[''id''] = self._id --> 628 return self._parent.execute(command, params) 629 630 def find_element(self, by=By.ID, value=None): ~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params) 318 response = self.command_executor.execute(driver_command, params) 319 if response: --> 320 self.error_handler.check_response(response) 321 response[''value''] = self._unwrap_value( 322 response.get(''value'', None)) ~/anaconda/envs/py36/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response) 240 alert_text = value[''alert''].get(''text'') 241 raise exception_class(message, screen, stacktrace, alert_text) --> 242 raise exception_class(message, screen, stacktrace) 243 244 def _value_or_default(self, obj, key, default): ElementNotVisibleException: Message: element not interactable (Session info: chrome=72.0.3626.109) (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.12.6 x86_64)


El elemento con texto como Cerrar es un elemento dinámico para localizar el elemento que tiene que inducir a WebDriverWait para que se pueda hacer clic en el elemento y puede usar cualquiera de las siguientes soluciones:

  • Usando CSS_SELECTOR :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.action-bar button.c-button.c-button--blue"))).click()

  • Utilizando XPATH :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[contains(@class, ''action-bar'')]//button[@class=''c-button c-button--blue'' and normalize-space()=''Close'']"))).click()

  • Nota : Tienes que agregar las siguientes importaciones:

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


Intente usar XPATHs y clase de acción para este tipo de escenarios.

BTN_xpath = //*[contains(@class, ''c-button--blue'')] WebElement btn = driver.find_element_by_xpath(BTN_xpath); Actions ac = new Actions(driver); ac.click(btn).build().perform();

Además, si está utilizando el navegador Firefox, podría haber problemas en los que no pueda interactuar con elementos que antes no estaban visibles en la página. Por lo tanto, intente verificar un con otro navegador también.