我想通过选择选择对象中的值来更改表中的行数。选择被 google iframe 遮挡。我可以点击 iframe 的“x”按钮,但 google iframe 仍然存在。
change_rows_el = driver.find_element(By.CSS_SELECTOR, 'select[aria-label="rows per page"')
ActionChains(driver).scroll_to_element(change_rows_el)
select = Select(change_rows_el)
select.select_by_value("50")
我明白了
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <select> is not clickable at point (1067,951) because another element <iframe id="google_ads.." > obscures it
如果我有一个按钮,我可能可以做类似的事情:
element = driver.find_element(By.ID, "element-id")
driver.execute_script("arguments[0].click();", element)
在这种情况下,模糊 iframe 可能不会产生影响。
请注意,我尝试使用
scroll_to_element
,但这并不重要,因为选择元素已经位于视口中。
下面的屏幕截图(选择位于“Google 广告”下):
这是为该表选择 50 行的一种方法:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time as t
chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')
chrome_options.add_argument("window-size=1280,1080")
with webdriver.Chrome(options=chrome_options) as driver:
driver.get('https://demoqa.com/webtables')
select_table_length = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@aria-label="rows per page"]'))))
select_table_length.select_by_visible_text("50 rows")
print('selected 50 rows')
t.sleep(100)
我添加了一些睡眠时间,以便让您看到结果。当然,您也可以在没有上下文管理器的情况下运行 Chrome。 Selenium 文档可以在here找到。