From 5ea7d37d2b47d7d98a1f19f8c59760b6a86a483b Mon Sep 17 00:00:00 2001 From: Panupong Pasupat Date: Sat, 22 Oct 2022 22:52:28 -0700 Subject: [PATCH 1/2] Make the code work with Python 3 + Selenium --- html/miniwob/click-dialog-2.html | 4 +- python/README.md | 2 +- python/miniwob/action.py | 16 +++-- python/miniwob/environment.py | 6 +- python/miniwob/fields.py | 18 ++--- python/miniwob/instance.py | 18 ++--- python/miniwob/screenshot.py | 8 +-- python/miniwob/state.py | 8 +-- python/miniwob/utils.py | 10 +-- python/requirements.txt | 5 +- python/test/test_action.py | 32 ++++----- python/test/test_environment.py | 120 +++++++++++++++---------------- record.py | 8 +-- 13 files changed, 127 insertions(+), 128 deletions(-) diff --git a/html/miniwob/click-dialog-2.html b/html/miniwob/click-dialog-2.html index 2dd3af0e..e5dfe32e 100644 --- a/html/miniwob/click-dialog-2.html +++ b/html/miniwob/click-dialog-2.html @@ -41,11 +41,11 @@ position: {my: 'center', at: 'center', of: document.getElementById('area')}, buttons: [ { text: 'Cancel', click: function(e) { - var r = e.toElement.innerHTML === expectedButton ? 1.0 : -1.0; + var r = e.target.innerHTML === expectedButton ? 1.0 : -1.0; core.endEpisode(r, r > 0); } }, { text: 'OK', click: function(e) { - var r = e.toElement.innerHTML === expectedButton ? 1.0 : -1.0; + var r = e.target.innerHTML === expectedButton ? 1.0 : -1.0; core.endEpisode(r, r > 0); } } ] diff --git a/python/README.md b/python/README.md index 8eaf056f..77140602 100644 --- a/python/README.md +++ b/python/README.md @@ -10,7 +10,7 @@ - Selenium - Outside this repository, download - [ChromeDriver](https://sites.google.com/a/chromium.org/chromedriver/downloads). + [ChromeDriver](https://chromedriver.chromium.org/downloads). Unzip it and then add the directory containing the `chromedriver` executable to the `PATH` environment variable ``` diff --git a/python/miniwob/action.py b/python/miniwob/action.py index 1cc131af..22eaae63 100644 --- a/python/miniwob/action.py +++ b/python/miniwob/action.py @@ -3,11 +3,11 @@ import logging from selenium.webdriver.common.action_chains import ActionChains +from selenium.webdriver.common.by import By -class MiniWoBAction(object): +class MiniWoBAction(metaclass=abc.ABCMeta): """Defines an action in its __call__ method.""" - __metaclass__ = abc.ABCMeta @abc.abstractmethod def __call__(self, driver): @@ -61,10 +61,12 @@ def __init__(self, left, top): def __call__(self, driver): """Clicks at coordinates (left, top)""" - body = driver.find_element_by_tag_name('body') + body = driver.find_element(By.TAG_NAME, 'body') + # The offset is from the center, not top-left. + x = - body.size['width'] / 2 + self.left + y = - body.size['height'] / 2 + self.top chain = ActionChains(driver) - chain.move_to_element_with_offset( - body, self.left, self.top).click().perform() + chain.move_to_element_with_offset(body, x, y).click().perform() @property def left(self): @@ -114,7 +116,7 @@ def __init__(self, element, fail_hard=False): def __call__(self, driver): if self.element.tag == 'select': # SPECIAL CASE: