Selenium nie klika na elementy z powodu plam na Słońcu?

0

TO, czy Selenium kliknie czy nie, zależy w tym snippecie od tego, który obrazek się zamieści! Jest to dla mnie niepojęte.

Niedziałający kod:


<!DOCTYPE html>
<html>
<head>
    <title>WTF</title>
    <meta charset="utf-8" />
    <script>
        function was_actually_clicked()
        {
            document.getElementById('v').innerHTML = '1';
        }
    </script>
</head>
<body>
<div id="v">0</div>
<map name="Map" id="Map">
    <area
            id="clickheredearselenium" alt="" title=""
            href="javascript:was_actually_clicked();"
            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="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.driver = WebDriver()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()
        super(SeleniumTest, cls).tearDownClass()

    def test_wtf(self):
        self.driver.get('%s%s' % (self.live_server_url, '/'))
        self.driver.find_element_by_id('clickheredearselenium').click()
        WebDriverWait(self.driver, 10).until(text_to_be_present_in_element((By.ID, "v"), "1"))
        self.assertEqual(self.driver.find_element_by_id('v').text, '1')

Selenium nie klika na area jak mu się każe: self.driver.find_element_by_id('clickheredearselenium').click()

Przez co następna linijka rzuca timeout:

Error
Traceback (most recent call last):
  File "/home/m/PycharmProjects/WTF/WTFApp/tests.py", line 22, in test_wtf
    WebDriverWait(self.driver, 10).until(text_to_be_present_in_element((By.ID, "v"), "1"))
  File "/home/m/p3/lib/python3.5/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Ale wystarczy ręcznie kliknąć na tę <area> w okienku przeglądarki, które wyskoczy podczas testowania, żeby testy przeszły.

Wystarczy także zmienić obrazek: Zamiast:

<img usemap="#Map" src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/POL_location_map.svg/500px-POL_location_map.svg.png" alt="pic">

Wstawić:

<img usemap="#Map" src="http://placehold.it/350x150" alt="pic">

Ażeby Selenium było łaskawe klikać na element i żeby testy przechodziły bez konieczności ręcznej interwerncji.

Geckodriver 0.18.0 Selenium 3.5.0 Firefox 55.0.2 Python 3.5.2 Serwer deweloperski: Django 1.11.4 Linux Mint 18 Cinnamon 64-bit

PS. Tak, to jest poprawny link to obrazka: https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/POL_location_map.svg/500px-POL_location_map.svg.png PPS. Nie, dziwaczność tego linku nie jest powodem niedziałąnia Selenium. Zapisanie obrazka na HDD i zmiana tego tagu na taki:

<img usemap="#Map" src="/static/500px-POL_location_map.png" alt="pic">

Nie rozwiązuje problemu.

Jakakolwiek szansa naprawienia tego? Przyznaję, że potrzebuję rozwiązać problem mocno pilnie.

0

Spróbuj:

self.driver.get('%s%s' % (self.live_server_url, '/'))
time.sleep(3)
self.driver.find_element_by_id('clickheredearselenium').click()
0

@Hummin:

Niestety guzik :(

0

A szukałeś w necie - pierwszy link mówi, że właśnie w selenium jest skopane klikanie area i nie działa https://stackoverflow.com/questions/11804926/is-there-a-way-to-click-an-area-in-an-image-map-using-seleniums-webdriver. Nie szukałem czy już to naprawili.
Workaround: zrób to w javascript - selenium ma metode execute_script czy jakoś tak.

0

@Hummin:

Tak szukałem, ale:

• Teraz przykładu nie podam, natomiast pamiętam, że analogiczne problemy miałem dawniej z innymi klikaniami w Selenium;
• Inny obrazek działa bez problemu;
• Tam jest post: Using Selenium 2.41, I was successful in clicking map area. Below, I have
tried to close lightbox image by clicking "X" which has action associated
with it and has placed on image using co-ordinates.

Może to problem z Firefoksem tylko biorąc pod uwagę wypowiedzi. Muszę przełknąć dumę i spróbować Chrome.

0

@kmph, trochę po czasie, ale może coś ci się przyda. przetestowałem u siebie, działa:

from django.contrib.staticfiles.testing import StaticLiveServerTestCase

from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import text_to_be_present_in_element
from selenium.webdriver.support.ui import WebDriverWait


class SeleniumTest(StaticLiveServerTestCase):
    def setUp(self):
        self.driver = WebDriver()

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

    def wait_for_element(self, by, value):
        try:
            element = WebDriverWait(self.driver, 10).until(
                EC.visibility_of_element_located(
                    getattr(By, "{}".format(by)), "{}".format(value))
                )
        except:
           pass

    def test_wtf(self):
        self.driver.get(self.live_server_url)
        self.wait_for_element('ID', 'clickheredearselenium')
        script = "document.getElementById('clickheredearselenium').click()"
        self.driver.execute_script(script)
        WebDriverWait(self.driver, 10).until(text_to_be_present_in_element((By.ID, "v"), "1"))
        self.assertEqual(self.driver.find_element_by_id('v').text, '1')

1 użytkowników online, w tym zalogowanych: 0, gości: 1