Skip to main content
 首页 » 编程设计

python之无论如何都找不到元素

2024年10月01日7duanxz

<iframe id="iframe10737" _color="main_gray" src="cn.edu.sufe.ext.stuAct.protal.queryActByType.flow?dlid=10737&amp;color=main_gray" width="100%" height="450px" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"> 
  #document 
    <html class="js cssanimations"> 
        <head>...</head> 
        <body style="background-color:#fff;" marginwidth="0" marginheight="0"> 
            <ul data-am-widget="gallery" class="am-gallery am-avg-sm-2   am-avg-md-5 am-avg-lg-4 am-gallery-bordered am-no-layout" data-am-gallery="{  }"> 
                  ::before 
                <li> 
                    <a href="/ext/cn.edu.sufe.ext.stuAct.actDetail.flow?id=28da6cbbe5d44b7a9074c26a0d1f22a0" target="_blank"> 
                        <div style="padding:10px 0px;"> 
                            <img src="cn.edu.sufe.common.util.FileDownload.flow?fileid=af55af7c42cd498c904d7a092945382b" style="width:170px;height:127px;" width="170px" height="127px"> 
                            <h3 class="am-gallery-title">Outdoor Sport Association</h3> 
                            <div class="am-gallery-desc">start time:</div> 
                            <div class="am-gallery-desc">end time:2018-01-17</div> 
                        </div> 
                    </a> 
                </li> 
                  ::after 
            </ul> 
        </body> 
</iframe> 

我需要找到元素并点击它:

<h3 class="am-gallery-title">Outdoor Sport Association</h3> 

试了很多,还是没解决。

driver.find_element_by_css_selector('h3[class="am-gallery-title"]').click() 
 
driver.find_element_by_class_name("am-gallery-title").click() 
 
driver.find_element_by_xpath('//*[contains(text(),"Outdoor Sport Association")]').click() 
 
driver.find_element_by_partial_link_text('Outdoor Sport Association').click() 
 
driver.find_element_by_css_selector('h3.Outdoor Sport Association').click() 
 
driver.find_element_by_css_selector('/body/ul/li/a/div/h3').click() 
 
driver.find_element_by_xpath('//*[@class="js cssanimations"]/body/div[2]/div/h1/a').click() 

那些方法根本不管用,都说:

NoSuchElementException: no such element: Unable to locate element:{...}

. 谁能给我一些建议?谢谢!!!

请您参考如下方法:

h3 元素位于 iframe 内。为了能够处理它,您需要先切换到那个 iframe:

driver.switch_to_frame('iframe10737') 
driver.find_element_by_xpath('//h3[text()="Outdoor Sport Association"]')