selenium中通过css定位

 以百度为例

html:

<body link="#0000cc" style="">
    <script>...</script>
    <div id="wrapper" style="display: block;">
        <script>...</script>
        <div id="head" class="">
            <div class="head_wrapper">
                <div class="s_form">
                    <div class="s_form_wrapper soutu-env-nomac soutu-env-index">
                        <style>...</style>
                        <div>
                        <a>
                        <form id="form" name="f" action="/s" class="fm">
                            <input>[7]
                            <span class="bg s_ipt_wr quickdelete-wrap ipthover">
                                <span class="soutu-btn"></span>
                                <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
                                <a>
                            </span>
                            <span class="bg s_btn_wr">
                                <input type="submit" id="su" value="百度一下" class="bg s_btn">
                            </span>

class选择器:ele=driver.find_element_by_css_selector(".s_ipt")

id选择器:ele=driver.find_element_by_css_selector("#kw")

ele=driver.find_element_by_css_selector("[maxlength='255']")

组合:ele=driver.find_element_by_css_selector("span.bg.s_ipt_wr.quickdelete-wrap>input#kw")

浏览器直接生成:

猜你喜欢

转载自www.cnblogs.com/Jasper-changing/p/12522296.html