如果我在img标签的src属性中放置了一个不同的图像,Selenium会跳过点击一个元素而无明显原因?
问题描述:
这太可笑了。为什么会发生?如果我在img标签的src属性中放置了一个不同的图像,Selenium会跳过点击一个元素而无明显原因?
HTML源代码:
<!DOCTYPE html>
<html>
<head>
<title>WTF</title>
<meta charset="utf-8" />
</head>
<body id="b">
<map name="Map" id="Map">
<area
id="clickhereyoustupidselenium" alt="" title=""
href="javascript:document.getElementById('b').innerHTML = 'adsf'"
shape="poly" coords="51,29,155,25,247,87,156,129,52,132,23,78,84,56,104,35" />
<img usemap="#Map" src="http://placehold.it/350x150" alt="350 x 150 pic">
</map>
</body>
</html>
硒测试代码:
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import text_to_be_present_in_element
from selenium.webdriver.common.by import By
class SeleniumTest(StaticLiveServerTestCase):
@classmethod
def setUpClass(cls):
super(SeleniumTest, cls).setUpClass()
cls.selenium = WebDriver()
@classmethod
def tearDownClass(cls):
cls.selenium.quit()
super(SeleniumTest, cls).tearDownClass()
def test_wtf(self):
self.selenium.get('%s%s' % (self.live_server_url, '/'))
self.selenium.find_element_by_id('clickhereyoustupidselenium').click()
WebDriverWait(self.selenium, 100).until(text_to_be_present_in_element((By.TAG_NAME, "body"), "adsf"))
self.assertEqual(self.selenium.find_element_by_tag_name('body').text, 'adsf')
测试精美通过。
好了,现在让我们来代替src="http://placehold.it/350x150"
具有不同的图像,让我们说这个人:src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/POL_location_map.svg/500px-POL_location_map.svg.png"
:
<!DOCTYPE html>
<html>
<head>
<title>WTF</title>
<meta charset="utf-8" />
</head>
<body id="b">
<map name="Map" id="Map">
<area
id="clickhereyoustupidselenium" alt="" title=""
href="javascript:document.getElementById('b').innerHTML = 'adsf'"
shape="poly" coords="51,29,155,25,247,87,156,129,52,132,23,78,84,56,104,35" />
<img usemap="#Map" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/POL_location_map.svg/500px-POL_location_map.svg.png" alt="350 x 150 pic">
</map>
</body>
</html>
让我们不要碰硒代码不是一个极小的微小位。
结果? Selenium提出:selenium.common.exceptions.TimeoutException
事实上,显示出来的Firefox窗口仍然显示波兰的地图,而不是'adsf'。如果我在Firefox窗口中点击此区域,直到100秒超时,Selenium立即结束测试。但它是硒应该点击这个元素!
发生了什么事以及如何阻止这种疯狂?
Geckodriver 0.18.0。硒3.5.0。 Firefox 55.0.2。 Python 3.5.2。而且,如果这很重要,开发服务器是Django 1.11.4。
答
根本原因是大小<area>
对GeckoDriver不正确。 Selenium WebDriver尝试点击元素的中间,但是区域的大小等于地图。所以Selenium点击错误的位置。
您可以计算位置并强制Selenium在该位置点击。见下面的代码。
area = driver.find_element_by_id('clickhereyoustupidselenium')
coords = area.get_attribute("coords").split(',')
coordsNumbers = [ int(p) for p in coords ]
x = filter(lambda p: p % 2 != 0, coordsNumbers)
y = filter(lambda p: p % 2 == 0, coordsNumbers)
middleX = (max(x) - min(x))/2
middley = (max(y) - min(y))/2
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(area, middleX, middley)
action.click()
action.perform()
WebDriverWait(driver, 100).until(EC.text_to_be_present_in_element((By.TAG_NAME, "body"), "adsf"))
print("Message found")
driver.quit()