2017-08-24 40 views
0

這太可笑了。爲什麼會發生?如果我在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。

回答

0

根本原因是大小<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() 
相關問題