📅  最后修改于: 2023-12-03 15:00:35.374000             🧑  作者: Mango
When using Selenium for web automation, you may encounter an exception called ElementNotVisibleException
. This usually happens when trying to interact with an element on a web page that is not currently visible, meaning it is hidden or not in view.
ElementNotVisibleException
There are several reasons why an element may not be visible:
ElementNotVisibleException
To handle ElementNotVisibleException
, you need to identify the cause of the problem and take appropriate action. Here are some steps you can take:
WebDriverWait
class in Selenium.execute_script
method.Here is an example code snippet that demonstrates how to handle ElementNotVisibleException
:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# Wait for element to become visible
wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, "my_element_id")))
# Scroll to the element
driver.execute_script("arguments[0].scrollIntoView();", element)
# Change the CSS to show the element
driver.execute_script("document.getElementById('my_element_id').style.display = 'block';")
# Switch to the iframe
driver.switch_to.frame("my_iframe_id")
In summary, ElementNotVisibleException
is a common issue in Selenium automation, but can be easily handled by identifying the cause and taking appropriate action.