使用 selenium 在 Selenium Python 中处理警报

Handle alert in Selenium Python with selenium

import openpyxl
from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.chrome.options import Options
wb = openpyxl.Workbook()
chrome_options = Options()


sheet = wb.active
sheet.append(["1lable", "2lable"])
wb.save("필름메이커스.xlsx")

path = "/Users/KimJinwoo/Desktop/도구/파이썬 작업공간./chromedriver 2"

driver = webdriver.Chrome(path)

driver.get('https://www.filmmakers.co.kr/actorsAudition/category/2343665')

driver.find_element_by_name("user_id").send_keys("3434343")
driver.find_element_by_name("password").send_keys("3434343")
driver.find_element_by_css_selector("#fo_login_widget > button").click()
driver.get('https://www.filmmakers.co.kr/actorsAudition/11445767?category=2343665')
lost = ['https://www.filmmakers.co.kr/actorsAudition/11636208?category=2343665']
list = ['https://www.filmmakers.co.kr/performerWanted/11638737/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11637736/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/1163693348/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11636933/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11636543/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11636508/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11636231/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11635838/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11635747/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11635719/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11635657/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11635481/page/1?category=8294214' , 
'https://www.filmmakers.co.kr/performerWanted/11635371/page/1?category=8294214']

for i in list: 
    driver.get(i)

    number = 0
    number = number +1
    for number in range(1,11):
        number = number +1
        abc = driver.find_element_by_css_selector("#board_content > div.board > table.ui.unstackable.striped.celled.table > tbody > tr:nth-child(%d) > td.three.wide.text-bold" %number)
        table = driver.find_element_by_css_selector("#board_content > div.board > table.ui.unstackable.striped.celled.table > tbody > tr:nth-child(%d) > td:nth-child(2)" %number)
        t = abc.text
        b = table.text
        print(t,b)
        sheet.append([t,b])
    wb.save("필름메이커스1.xlsx")
driver.quit()

Traceback (most recent call last): File "/Users/KimJinwoo/잡파일/크롤링 - 필름메이커스.py", line 45, in abc = driver.find_element_by_css_selector("#board_content > div.board > table.ui.unstackable.striped.celled.table > tbody > tr:nth-child(%d) > td.three.wide.text-bold" %number) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 802, in find_element_by_css_selector return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 1238, in find_element return self.execute(Command.FIND_ELEMENT, { File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 418, in execute self.error_handler.check_response(response) File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace, alert_text) # type: ignore[call-arg] # mypy is not smart enough here selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: 대상을 찾을 수 없습니다. Message: unexpected alert open: {Alert text : 대상을 찾을 수 없습니다.} (Session info: chrome=95.0.4638.69) Stacktrace: 0 chromedriver 2 0x00000001004ff89c __gxx_personality_v0 + 538736 1 chromedriver 2 0x0000000100495b64 __gxx_personality_v0 + 105272 2 chromedriver 2 0x000000010009ead0 chromedriver 2 + 158416 3 chromedriver 2 0x00000001000f720c chromedriver 2 + 520716 4 chromedriver 2 0x00000001000c34d8 chromedriver 2 + 308440 5 chromedriver 2 0x00000001004c2ca0 __gxx_personality_v0 + 289908 6 chromedriver 2 0x00000001004d6dc8 __gxx_personality_v0 + 372124 7 chromedriver 2 0x00000001004db94c __gxx_personality_v0 + 391456 8 chromedriver 2 0x00000001004d7d04 __gxx_personality_v0 + 376024 9 chromedriver 2 0x00000001004b8c88 __gxx_personality_v0 + 248924 10 chromedriver 2 0x00000001004f0bd0 __gxx_personality_v0 + 478116 11 chromedriver 2 0x00000001004f0d44 __gxx_personality_v0 + 478488 12 chromedriver 2 0x0000000100506458 __gxx_personality_v0 + 566316 13 libsystem_pthread.dylib 0x00000001880294ec _pthread_start + 148 14 libsystem_pthread.dylib 0x00000001880242d0 thread_start + 8

这是错误 我如何编辑代码?

for i in list: 
    driver.get(i)
    number = 0
    number = number +1
    for number in range(1,11):
        number = number +1
        try:
            abc = driver.find_element_by_css_selector("#board_content > div.board > table.ui.unstackable.striped.celled.table > tbody > tr:nth-child(%d) > td.three.wide.text-bold" %number)
            table = driver.find_element_by_css_selector("#board_content > div.board > table.ui.unstackable.striped.celled.table > tbody > tr:nth-child(%d) > td:nth-child(2)" %number)
            t = abc.text
            b = table.text
            print(t,b)
        except:
            pass

您没有在页面上找到这些变量,所以请尝试将它们包装起来,除了。