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:\SeleniumDriver\chromedriver.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/close 按鈕??將具有路徑:
/html/body/div[6]/div[1]/a
但是,如果您打開下一個鏈接,路徑將如下所示:
/html/body/div[8]/div[1]/a
讓我知道你對此的看法:-)
uj5u.com熱心網友回復:
這是實作目標的一種方法:
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[]')))[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/找到
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/506275.html
上一篇:js重繪進度條
下一篇:VBA僅找到1個匹配項/出現
