python - 无论如何找不到该元素

标签 python selenium find webdriver element

<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"]')

关于python - 无论如何找不到该元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42312576/

相关文章:

python - 检查列表中是否至少存在一个字符串

python - Aerospike Python 客户端中的段错误

linux - 如何删除具有路径模式的所有文件

jquery - 使用 jQuery Accordion 时在页面上查找字符串 (Ctrl+F)

Python - 返回 None

Python 3、以太坊——如何发送 ERC20 代币?

python - 大页面上的 Selenium 卡住

java - 我得到 false 作为 boolean 值,但输出总是显示我成功而不是不成功。谁能告诉我这有什么问题

java - 以编程方式检查页面状态

c++ - 以 vector 为键操作的 map 查找