2017-08-24 1 views
0

これはばかげている。なぜそれが起こるのですか?imgタグのsrc属性に別のイメージを配置すると、セレニウムが要素をクリックしてスキップしても何の問題もありません。

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> 

Seleniumテストコード:

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="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/POL_location_map.svg/500px-POL_location_map.svg.png"

OK、今のは違うイメージでsrc="http://placehold.it/350x150"に代わって、のはこの1つを言わせ

<!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 raises:selenium.common.exceptions.TimeoutException

実際に表示されるFirefoxウィンドウには、まだ「adsf」ではなく、ポーランドの地図が表示されます。 100秒のタイムアウトが経過するまでに表示されるFirefoxウィンドウのこの領域をクリックすると、Seleniumはテストが合格したとすぐに判断します。しかし、この要素をクリックするはずだったのはセレンでした!

この狂気は何が起こっているのですか? 0130。セレン3.5.0。 Firefox 55.0.2。 Python 3.5.2。そして、これが問題ならば、devサーバはDjango 1.11.4です。

答えて

0

GeckoDriverの根本的な原因は、<area>のサイズが正しくありません。 Selenium WebDriverは要素の中央をクリックしようとしますが、領域のサイズはマップと等しくなります。セレンは間違った位置をクリックします。
位置を計算して、その位置で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() 
関連する問題