2017-08-24 1 views
0

Das ist lächerlich. Warum passiert das??Selen springt beim Klicken auf ein Element ohne ersichtlichen Grund, wenn ich ein anderes Bild in das Attribut src des Tags img platziere?

HTML-Quellcode:

<!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> 

Selen Testcode:

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') 

Der Test schön passiert.

OK, so jetzt lassen Sie uns src="http://placehold.it/350x150" mit einem anderen Bild ersetzen, sagen wir mal dieses: 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> 

Lassen Sie uns nicht Selen Code nicht ein winzig kleines bisschen berühren.

Ergebnis? Selen wirft: selenium.common.exceptions.TimeoutException

Und in der Tat, zeigt das Firefox-Fenster, zeigt immer noch die Karte von Polen, und nicht "adsf". Wenn ich auf diesen Bereich im Firefox-Fenster klicke, das bis zum Timeout von 100 Sekunden angezeigt wird, schließt Selenium sofort, dass der Test bestanden wurde. Aber es war Selenium, das auf dieses Element klicken sollte !!

Was passiert und wie kann man diesen Wahnsinn stoppen?

Geckodriver 0.18.0. Selen 3.5.0. Firefox 55.0.2. Python 3.5.2. Und wenn das wichtig ist, ist der Dev-Server Django 1.11.4.

Antwort

0

Die Ursache ist die Größe von <area> auf GeckoDriver ist falsch. Selenium WebDriver versucht, auf die Mitte des Elements zu klicken, aber die Größe des Bereichs entspricht der Karte. Selen klickt also auf eine falsche Position.
Sie können die Position berechnen und Selen zwingen, auf die Position zu klicken. Siehe Code unten.

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() 
Verwandte Themen