循环迭代时的第二个'Element not interactable exception',Selenium PYTHON



Selenium在第二次迭代中确实看到页面上显示的元素可见

我点击一个链接,就会出现一个网站中的框。我需要关上那个箱子。

此操作将执行1000+次。在第一次迭代中,Selenium打开链接并关闭框。在第二次迭代中,Selenium打开链接,但无法关闭框。此时,它给出错误信息:

Exception has occurred: ElementNotInteractableException Message: element not interactable (Session info: chrome=105.0.5195.102)

我的代码+下面相关元素的HTML。

from selenium import webdriver
from selenium.webdriver.common.by import By    
import time
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r"D:SeleniumDriverchromedriver.exe")
driver.get('https://sprawozdaniaopp.niw.gov.pl/') 
find_button = driver.find_element("id", "btnsearch")
find_button.click()
interesting_links = driver.find_elements(By.CLASS_NAME, "dialog")
for i in range(len(interesting_links)):
interesting_links[i].click()
time.sleep(10)                           # I tried 60 seconds, no change
#
#   HERE I WOULD DO MY THINGS
#
close_box = driver.find_element(By.CLASS_NAME, "ui-dialog-titlebar-close")

print(close_box.is_displayed())
close_box.click()                        # Here is where the program crushes on the 2nd iteration
if i == 4:                               # Stop the program after 5 iterations
break

相关元素的HTML代码:

<a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button"><span class="ui-icon ui-icon-closethick">close</span></a>

我试图通过CSS SELECTOR AND XPATH找到关闭框的元素。

  • X/close按钮的CSS SELECTOR每次都是一样的,但是只有Selenium第一次看到X按钮显示
  • XPATH很奇怪。第一次打开链接时,X/关闭按钮将具有路径:

/html/body/div[6]/div[1]/a

然而,如果您打开下一个链接,路径将如下所示:

/html/body/div[8]/div[1]/a

让我知道你对此的看法:-(

这是实现目标的一种方法:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
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,720")
webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
url = 'https://sprawozdaniaopp.niw.gov.pl/'
browser.get(url)
wait.until(EC.element_to_be_clickable((By.ID, "btnsearch"))).click()
links = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "a[class='dialog']")))
counter = 0
for link in links[:5]:
link.click()
print('clicked link', link.text)
### do your stuff ###
t.sleep(1)
wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, 'span[class="ui-icon ui-icon-closethick"]')))[counter].click()
print('closed the popup')
counter = counter+1

这将在终端中打印出来:

clicked link STOWARZYSZENIE POMOCY DZIECIOM Z PORAŻENIEM MÓZGOWYM "JASNY CEL"
closed the popup
clicked link FUNDACJA NA RZECZ POMOCY DZIECIOM Z GRODZIEŃSZCZYZNY
closed the popup
clicked link FUNDACJA "ADAMA"
closed the popup
clicked link KUJAWSKO-POMORSKI ZWIĄZEK LEKKIEJ ATLETYKI
closed the popup
clicked link "RYBNICKI KLUB PIŁKARSKI - SZKÓŁKA PIŁKARSKA ROW W RYBNIKU"
closed the popup

每次单击链接时,都会创建一个新的弹出窗口。当你关闭它时,弹出窗口不会消失,但会保持隐藏状态。因此,当你点击一个新的链接,然后你想关闭新的弹出窗口时,你需要选择新的(第n个(关闭按钮。这也应该适用于弹出元素,所以请确保你考虑到它。我在第5个链接后停止了,当然你需要删除切片来处理页面中的所有链接。上面的Selenium设置是linux上的chromedriver——您只需要观察导入,以及定义浏览器(驱动程序(后的代码。

Selenium文档可在https://www.selenium.dev/documentation/

最新更新