【问题标题】:Can't click item with Selenium - Python无法使用 Selenium 单击项目 - Python
【发布时间】:2021-09-08 10:38:47
【问题描述】:

所以我有这个运行正常的脚本,然后停止工作。 运行脚本时未点击该项目并出现此错误。

Traceback (most recent call last):
 File "/Users/6ko/Desktop/CodePython/katas/kata3.py", line 47, in <module>
   actions.perform()
 File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/action_chains.py", line 80, in perform
   self.w3c_actions.perform()
 File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/common/actions/action_builder.py", line 76, in perform
   self.driver.execute(Command.W3C_ACTIONS, enc)
 File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
   self.error_handler.check_response(response)
 File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
   raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.MoveTargetOutOfBoundsException: Message: move target out of bounds 

这是我的代码:


edu = driver.find_element_by_id("118735")
ActionChains(driver).click(edu).perform()

这是网站的元素代码:


    <div class="tnt-item" style="cursor: pointer; pointer-events: auto;" id="118735">
                  <div class="song" style="opacity: 1;">
                    <div class="position">17</div>
                    <div class="song-cover" style="position:relative;"><img class="img-fluid" src="/upload/album/00000036404.jpg"></div>
                    <div class="song-info">
                      <div class="song-name">Love You Better</div>
                      <div class="song-artists"><span title="Edu Monteiro">Edu Monteiro</span></div>
                      <div class="tnt-listen-music">
                        <div class="audioPlayer sm2-bar-ui small">
                          <div class="bd sm2-main-controls">
                            <div class="sm2-inline-element sm2-button-element">
                              <div class="sm2-button-bd"><a class="sm2-inline-button play-pause tm-30sec-listen" href="#play" id="TNT">
                            Play / pause</a></div>
                            </div>
                            <div class="sm2-inline-element sm2-inline-status">
                              <div class="sm2-playlist">
                                <div class="sm2-playlist-target"><ul class="sm2-playlist-bd"><li></li></ul></div>
                              </div>
                              <div class="sm2-progress">
                                <div class="sm2-progress-bd">
                                  <div class="sm2-progress-track">
                                    <div class="sm2-progress-bar"></div>
                                    <div class="sm2-progress-ball">
                                      <div class="icon-overlay"></div>
                                    </div>
                                  </div>
                                </div>
                                <div class="sm2-inline-time">0:00</div>
                                <div class="sm2-inline-duration">0:00</div>
                              </div>
                            </div>
                          </div>
                          <div class="bd sm2-playlist-drawer sm2-element">
                            <div class="sm2-playlist-wrapper">
                              <ul class="sm2-playlist-bd">
                                <li class="selected"><a id="" class="norewrite exclude button-exclude inline-exclude audiofile" style="font-size:0px;" isexclusiveplay="true" isclip="true" href="9x4h6qd1-48y7-5fxt-q5ll-dknvbm4r42f5.mp4"></a></li>
                              </ul>
                            </div>
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="song-check"><input type="checkbox" name="tnt-vote" class="tnt-vote" style="pointer-events:none;"></div>
                  </div>
                </div>

你能帮帮我吗?已经搜索过类似的问题,但找不到我的问题的解决方案。 非常感谢您的宝贵时间!

(脚本是投一首音乐,这是网站https://radiocomercial.iol.pt/programas/tnt

【问题讨论】:

    标签: python html selenium selenium-webdriver automation


    【解决方案1】:

    您是否尝试先移动到元素?

    edu = driver.find_element_by_id("118735")
    try:
        ActionChains(driver).move_to_element(edu).perform()
    except Exception as e:
        print("Exception :", str(e))
    #edu.click() selenium.common.exceptions.ElementClickInterceptedException
    driver.execute_script("arguments[0].click();", edu)
    

    【讨论】:

    • 我试过了,它给了我这个错误:selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (600, 3505)这是否意味着我试图点击的项目是不可点击的?
    • @6ko 我更新了答案,尝试使用 javascript 点击元素。
    • 成功了!非常感谢您的参与!!真的很感激:))
    猜你喜欢
    • 2022-01-07
    • 2017-05-29
    • 2018-05-26
    • 2021-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多