加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python selenium点击第n个元素

发布时间:2020-12-16 21:31:58 所属栏目:Python 来源:网络整理
导读:下面是检查代码,当鼠标位于图像上方时,基本上我想要点击图像…. ul id="product-list" li class="product one-col new" ul li class="image" title="sample image" a href="#product/1d77e790-f74a-3859-97db-c513cbece39c" img width="" height="" alt="" s
下面是检查代码,当鼠标位于图像上方时,基本上我想要点击图像….
<ul id="product-list">

    <li class="product one-col new">
        <ul>
            <li class="image" title="sample image">
                <a href="#product/1d77e790-f74a-3859-97db-c513cbece39c">
                    <img width="" height="" alt="" src="/content/images/1.jpg"></img>
                    <span class="new"> … </span>
                    <span class="hover"></span>
                </a>
                <p class="retailer"> … </p>
                <p class="brand"></p>
            </li>
            <li class="price"> … </li>
            <li class="name" title="sample image"> … </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>

我正在使用python selenium,并尝试了下面的单击span(悬停)链接

browser.find_element_by_css_selector("ul#product-list > :first-child > ul > li.image > a > span.hover ").click

但这不起作用……任何想法?

更新:

browser.find_element_by_css_selector("ul#product-list > :first-child > ul > li.image > a > span.hover ").click()


 File "/usr/lib/python2.7/site-packages/selenium-2.35.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py",line 164,in check_response
    raise exception_class(message,screen,stacktrace)
ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace: 
    at fxdriver.preconditions.visible (file:///tmp/tmp6Pgi9F/extensions/fxdriver@googlecode.com/components/command_processor.js:8231)
    at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmp6Pgi9F/extensions/fxdriver@googlecode.com/components/command_processor.js:10823)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmp6Pgi9F/extensions/fxdriver@googlecode.com/components/command_processor.js:10840)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmp6Pgi9F/extensions/fxdriver@googlecode.com/components/command_processor.js:10845)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmp6Pgi9F/extensions/fxdriver@googlecode.com/components/command_processor.js:10787)

更新:

这也行不通

browser.find_element_by_css_selector("ul#product-list > :first-child > ul > li.image > a ").click()

更新:

还尝试了动作链,鼠标点击..但没有运气..

element = browser.find_element_by_css_selector("ul#product-list > :first-child > ul > li.image")
    hov = ActionChains(browser).move_to_element(element)
    hov.click()

解决了:
最后这个工作……

element_to_hover_over = driver.find_element_by_css_selector("ul#product-list > :first-child ")
hover = ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
if "" == driver.find_element_by_css_selector("span.hover").text:
    driver.find_element_by_css_selector("span.hover").click()

解决方法

你的代码缺失().如果没有(),则不会调用click方法.
browser.find_element_by_css_selector("ul...span.hover ").click()
#                                                             ^^
element = browser.find_element_by_css_selector("ul#product-list > :first-child > ul > li.image > a > span.hover ")
browser.execute_script("arguments[0].innerText = 'asdf';",element)
element.click()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读