抓取新闻文章时找不到问题

0 投票
1 回答
24 浏览
提问于 2025-04-13 13:34

我有一段代码,关于日期的部分,虽然我检查过XPath的输出是正确的,但它却显示错误。而且我在id ="aswift_7"的地方遇到了一个可点击的错误。

# To open headline
for i in range(13, noOfArticles + 1):
    try:
        elem = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//section[@class='col-md-8 content']/a[" + str(i) + "]")))
        if elem.is_displayed():
            elem.click()
            time.sleep(1)
            headline = driver.find_element(By.XPATH, "//section[@class='col-md-8 content']/h1")
            # Process headline and date here
    except Exception as e:
        print("An error occurred:", e)
        # Handle the exception if needed
An error occurred: Message: element click intercepted: Element ... is not clickable at point (411, 235). Other element would receive the click: <iframe id="aswift_7" name="aswift_7" style="width: 100vw !important; height: 100vh !important; inset: 0px auto auto 0px !important; position: absolute !important; clear: none !important; display: inline !important; float: none !important; margin: 0px !important; max-height: none !important; max-width: none !important; opacity: 1 !important; overflow: visible !important; padding: 0px !important; vertical-align: baseline !important; visibility: visible !important; z-index: auto !important;" sandbox="allow-forms allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-top-navigation-by-user-activation" width="" height="" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" allowtransparency="true" scrolling="no" src="https://googleads.g.doubleclick.net/pagead/html/r20240319/r20110914/zrt_lookup_fy2021.html#RS-0-&amp;adk=1812271808&amp;client=ca-pub-7790868726705258&amp;fa=8&amp;ifi=9&amp;uci=a!9" data-google-container-id="a!9" data-google-query-id="COf7mcCDhYUDFfkhtwAdhhYEpg" data-load-complete="true"></iframe>(Session info: chrome=122.0.6261.129)

Stacktrace:
GetHandleVerifier [0x00007FF6BA2EAD02+56930](No symbol) 
[0x00007FF6BA25F602](No symbol) 
[0x00007FF6BA1142E5](No symbol)
 [0x00007FF6BA160A80](No symbol) 
[0x00007FF6BA15E8AB](No symbol) 
[0x00007FF6BA15C0B4](No symbol) 
[0x00007FF6BA15AE45](No symbol)
 [0x00007FF6BA14F798](No symbol)
 [0x00007FF6BA17BC9A](No symbol)
 [0x00007FF6BA14F09A](No symbol)
 [0x00007FF6BA17BEB0](No symbol) 
[0x00007FF6BA1981E2](No symbol)
 [0x00007FF6BA17BA43](No symbol)
 [0x00007FF6BA14D438](No symbol)
 [0x00007FF6BA14E4D1]GetHandleVerifier 
[0x00007FF6BA666F8D+3711213]GetHandleVerifier
 [0x00007FF6BA6C04CD+4077101]GetHandleVerifier
 [0x00007FF6BA6B865F+4044735]GetHandleVerifier 
[0x00007FF6BA389736+706710](No symbol) 
[0x00007FF6BA26B8DF](No symbol)
 [0x00007FF6BA266AC4](No symbol)
 [0x00007FF6BA266C1C](No symbol) 
[0x00007FF6BA2568D4]BaseThreadInitThunk
 [0x00007FFA46A6257D+29]RtlUserThreadStart 
[0x00007FFA47EEAA58+40]

1 个回答

0

主要问题在于iframe。你需要先切换到那个iframe,然后才能和里面的内容进行互动。Selenium把iframe看作是主页面中的一个独立窗口或框架。所以你需要先切换到这个iframe。

iframe = driver.find_element(By.ID, "aswift_7")
driver.switch_to.frame(iframe)

撰写回答