python selenium css 选择器第 n 个元素

标签 python linux selenium

我正在尝试在 selenium 上解析这段代码,基本上想要列表中的第一项,即“product one-col new”!点击...

<ul id="product-list">

    <li class="product one-col new">
        <ul>
            <li class="image" title="one item 5">
                <a href="#product/0f37f703-ed9a-3d10-86c0-7d0c0fcdb9ec">
                    <img width="" height="" alt="" src="/content/images/ProductImages/you-are-here/3/E/4/3E42561208174E6FBC81A10CDAE48659.jpg"></img>
                    <span class="new"> … </span>
                    <span class="hover"></span>
                </a>
                <p class="retailer">

                    CHECK ME

                </p>
                <p class="brand"></p>
            </li>
            <li class="price"> … </li>
            <li class="name" title="one item 5"> … </li>
            <li class="first-seen"> … </li>
        </ul>
    </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>
    <li class="product one-col new"> … </li>

我试过了

browser.find_element_by_css_selector("ul[id='product-list']  li:nth-of-type(1)").click()

但它不起作用?

有什么想法吗?

更新:

正确的路径是

browser.find_element_by_css_selector("ul#product-list > :first-child").text

最佳答案

看起来你想要一个直接的 child ,所以选择器应该使用 child 组合器。另外,还有 :first-of-type/:first-child:

#product-list > :first-child

关于python selenium css 选择器第 n 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19326204/

相关文章:

python - 使用 Python 在 ElasticSearch 中添加 @timestamp 字段

android - 使用挂载命令在 SnapdragonAPQ8074 中挂载 USB 驱动器

python - 为什么我的环境变量无法加载到我的 Flask 应用程序中?

linux - 在 Perl 中从线程更新标签

ruby - 在带有 Selenium for Ruby on Rails 的 Heroku 上找不到 Chrome 二进制文件

selenium - 以另一种方式检查 'checkBox' -> xPath

python - 在 Ubuntu 服务器上安装软件包时出现内存不足问题

python - 使用现有的 psycopg2 连接池创建一个 sqlalchemy 引擎

python - 在空格后拆分字符串

python - 如何在 AngularJS 网站上的 Python 中使用 Selenium 迭代和保存动态表中的信息