selenium python中的“元素不可点击”错误

时间:2016-06-22 10:16:24

标签: python selenium

我使用相同的脚本超过一年,但从昨天开始,当我点击有图像的链接时,我收到此错误。我通过xpath获取元素然后单击它。

test_101_HomePage_links (__main__.SprintTests) ... ERROR

======================================================================
ERROR: test_101_HomePage_links (__main__.SprintTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\Zaakpay\website\sanity results\debug\tests.py", line 17, in test_101_HomePage_links
    a.click()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 73, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py", line 456, in _execute
    return self._parent.execute(command, params)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: Element is not clickable at point (281.25, 61). Other element would receive the click: <span></span>


----------------------------------------------------------------------
Ran 1 test in 23.062s

FAILED (errors=1)

其他信息: 使用窗户, 使用python, 使用firefox, 相同的脚本在昨天工作正常

我的代码:

import unittest
from selenium import webdriver
import datetime
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import time

class SprintTests(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        self.driver.get("https://www.zaakpay.com")

    def test_101_HomePage_links(self):
        a= self.driver.find_element_by_xpath("/html/body/div[5]/div[1]/div[3]/ul/li[1]/a/i")
        a.click()
        time.sleep(5)
        a = self.driver.find_element_by_xpath('//*[@id="view1"]/p')
        b=a.text
        self.assertEqual('-Payment Gateway Services.\n-More than you want payment options with.\n-major credit cards, debit cards and 52 netbanking banks.\n-Fastest Merchant Approval.\n-Smooth integration across 22 platforms.\n-Start in minutes.\n-Multi-Currency Processing Service with 13 currencies.\n\nSIGN UP',b )


    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main(verbosity=2)

链接我试图点击是文本上方的圆形图像 - 网站付款网关

1 个答案:

答案 0 :(得分:1)

出于多种原因,使用像这样的/html/body/div[5]/div[1]/div[3]/ul/li[1]/a/i这样的XPATH是有问题的。

首先,它们非常脆弱。所需要的只是对网站的一个小的更新,一个甚至可能在夜间视觉上可感知,并且硬编码的div索引中断。网站开发者可能添加了一个div来稍微改变一种字体样式,然后你的XPATH就完全坏了。

其次,一旦它破坏,它几乎不可能进行调试。我完全不知道你打算点击什么,如果这是其他人试图修复的生产代码,他们也不知道你打算点击什么。

我最好的猜测是,zaakpay做了一些小的,难以察觉的变化,稍微改变了DOM中目标元素的位置。如果您将XPATH更新为类似//div[@class=\"prdtBlck\"]/ul/li/a的内容,正如我在下面所做的那样,您的脚本可以运行:

import unittest
from selenium import webdriver
import datetime
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import time

class SprintTests(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()
        self.driver.get("https://www.zaakpay.com")

    def test_101_HomePage_links(self):
        a=self.driver.find_element_by_xpath("//div[@class=\"prdtBlck\"]/ul/li/a")
        a.click()
        time.sleep(5)
        a = self.driver.find_element_by_xpath('//*[@id="view1"]/p')
        b=a.text
        self.assertEqual('-Payment Gateway Services.\n-More than you want payment options with.\n-major credit cards, debit cards and 52 netbanking banks.\n-Fastest Merchant Approval.\n-Smooth integration across 22 platforms.\n-Start in minutes.\n-Multi-Currency Processing Service with 13 currencies.\n\nSIGN UP',b )


    def tearDown(self):
        self.driver.quit()

if __name__ == '__main__':
    unittest.main(verbosity=2)