Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the code work with Python 3 + latest Selenium #7

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions html/miniwob/click-dialog-2.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
} }
]
Expand Down
2 changes: 1 addition & 1 deletion python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
16 changes: 9 additions & 7 deletions python/miniwob/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -114,7 +116,7 @@ def __init__(self, element, fail_hard=False):
def __call__(self, driver):
if self.element.tag == 'select':
# SPECIAL CASE: <select>
body = driver.find_element_by_tag_name('body')
body = driver.find_element(By.TAG_NAME, 'body')
chain = ActionChains(driver)
chain.move_to_element_with_offset(
body, self.element.left + 5,
Expand All @@ -126,7 +128,7 @@ def __call__(self, driver):
if self._fail_hard:
raise RuntimeError('{} failed: {}'.format(self, result))
else:
logging.warn('%s failed: %s', self, result)
logging.warning('%s failed: %s', self, result)

@property
def ref(self):
Expand Down
6 changes: 3 additions & 3 deletions python/miniwob/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def configure(self, num_instances=1, seeds=None, **kwargs):
for instance in self.instances:
instance.close()
self.instances = []
for index in xrange(num_instances):
for index in range(num_instances):
logging.info('Starting WebDriver Instance %d', index)
instance = MiniWoBInstance(
index, self.subdomain, seeds[index], **kwargs)
Expand Down Expand Up @@ -176,13 +176,13 @@ def test_environment():
try:
task_name = sys.argv[1]
except IndexError:
print 'Usage: python {} TASK_NAME'.format(sys.argv[0])
print('Usage: python {} TASK_NAME'.format(sys.argv[0]))
exit(1)
env = MiniWoBEnvironment(task_name)
base_url = os.environ.get('MINIWOB_BASE_URL')
env.configure(num_instances=1, seeds=[0], base_url=base_url)
states = env.reset()
print states[0].dom.visualize()
print(states[0].dom.visualize())
env.close()

if __name__ == '__main__':
Expand Down
18 changes: 9 additions & 9 deletions python/miniwob/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ def __len__(self):

@property
def keys(self):
return self._d.keys()
return list(self._d.keys())

@property
def values(self):
return self._d.values()
return list(self._d.values())

def __repr__(self):
return '\n'.join(u'{}: {}'.format(k, repr(v)) for k, v in self._d.items())
return '\n'.join('{}: {}'.format(k, repr(v)) for k, v in self._d.items())
__str__ = __repr__


Expand Down Expand Up @@ -134,7 +134,7 @@ def extract_click_checkboxes(utterance):
else:
targets = re.split(', ?', targets)
fields = dict(zip(
["target {}".format(i) for i in xrange(len(targets))], targets))
["target {}".format(i) for i in range(len(targets))], targets))
fields["button"] = "submit"
return Fields(fields)
FIELD_EXTRACTORS['click-checkboxes'] = extract_click_checkboxes
Expand All @@ -156,7 +156,7 @@ def extract_click_checkboxes_soft(utterance):
targets = re.match(r'Select words similar to (.*) and click Submit\.', utterance).group(1)
targets = re.split(', ?', targets)
fields = dict(zip(
["target {}".format(i) for i in xrange(len(targets))], targets))
["target {}".format(i) for i in range(len(targets))], targets))
fields["button"] = "submit"
return Fields(fields)
FIELD_EXTRACTORS['click-checkboxes-soft'] = extract_click_checkboxes_soft
Expand Down Expand Up @@ -900,17 +900,17 @@ def extract_utterances():
try:
task_name = sys.argv[1]
except:
print >> sys.stderr, 'Usage: {} task_name'.format(sys.argv[0])
print('Usage: {} task_name'.format(sys.argv[0]), file=sys.stderr)
exit(1)
FIELD_EXTRACTORS[task_name] = lambda utt: Fields({})
from miniwob.environment import MiniWoBEnvironment
env = MiniWoBEnvironment(task_name)
base_url = os.environ.get('MINIWOB_BASE_URL')
env.configure(num_instances=4, seeds=range(4), base_url=base_url)
for i in xrange(25):
env.configure(num_instances=4, seeds=list(range(4)), base_url=base_url)
for i in range(25):
states = env.reset()
for state in states:
print 'UTT:\t{}'.format(state.utterance.replace('\n', ' '))
print('UTT:\t{}'.format(state.utterance.replace('\n', ' ')))
env.close()

if __name__ == '__main__':
Expand Down
18 changes: 7 additions & 11 deletions python/miniwob/instance.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import json
import logging
from Queue import Queue
from queue import Queue
import time
import traceback
import urlparse
import urllib.parse
from threading import Thread

import numpy as np
Expand Down Expand Up @@ -76,14 +76,14 @@ def __init__(self, index, subdomain, seed, headless=False,
assert not base_url.startswith('file://'),\
('For {} domain, MINIWOB_BASE_URL cannot be file://. '
' See "Run a simple server" in README').format(subdomain)
self.url = urlparse.urljoin(base_url,
self.url = urllib.parse.urljoin(base_url,
subdomain.replace('.', '/') + '/wrapper.html')
self.window_width = self.FLIGHT_WINDOW_WIDTH
self.window_height = self.FLIGHT_WINDOW_HEIGHT
self.task_width = self.FLIGHT_TASK_WIDTH
self.task_height = self.FLIGHT_TASK_HEIGHT
else:
self.url = urlparse.urljoin(base_url,
self.url = urllib.parse.urljoin(base_url,
'miniwob/{}.html'.format(subdomain))
self.window_width = self.WINDOW_WIDTH
self.window_height = self.WINDOW_HEIGHT
Expand Down Expand Up @@ -156,7 +156,7 @@ def create_driver(self):
.format(self.window_width, self.window_height))
options.add_argument('window-position={},{}'
.format(9000, 30 + self.index * (self.window_height + 30)))
self.driver = webdriver.Chrome(chrome_options=options)
self.driver = webdriver.Chrome(options=options)
self.driver.implicitly_wait(5)
if self.headless:
self.driver.get(self.url)
Expand Down Expand Up @@ -247,13 +247,9 @@ def begin_task(self, seed=None):
if seed is not None:
self.set_seed(seed)
self.set_mode(self.mode)
# Wait for the sync screen, then click on it
#element = WebDriverWait(self.driver, 5).until(
# EC.element_to_be_clickable((By.ID, self.SYNC_SCREEN_ID)))
#self.driver.find_element_by_id(self.SYNC_SCREEN_ID).click()
self.driver.execute_script('core.startEpisodeReal();')
if self.block_on_reset:
for _ in xrange(self.RESET_BLOCK_MAX_ATTEMPT):
for _ in range(self.RESET_BLOCK_MAX_ATTEMPT):
if self.driver.execute_script('return WOB_TASK_READY;'):
break
time.sleep(self.RESET_BLOCK_SLEEP_TIME)
Expand All @@ -274,7 +270,7 @@ def perform(self, action):
"""
if action is not None:
if self.get_metadata()['done']:
logging.warn('Cannot call %s on instance %d, which is already done',
logging.warning('Cannot call %s on instance %d, which is already done',
action, self.index)
else:
action(self.driver)
Expand Down
8 changes: 4 additions & 4 deletions python/miniwob/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy as np

from cStringIO import StringIO
from io import StringIO
from PIL import Image, ImageDraw


Expand Down Expand Up @@ -79,8 +79,8 @@ def create_gif(path_prefix):

if __name__ == '__main__':
if len(sys.argv) != 2:
print 'Usage: {} PATH_PREFIX'.format(sys.argv[0])
print ' where PATH_PREFIX is something like '\
'data/experiments/123_unnamed/traces/test/2000-img/2000-3'
print('Usage: {} PATH_PREFIX'.format(sys.argv[0]))
print(' where PATH_PREFIX is something like '
'data/experiments/123_unnamed/traces/test/2000-img/2000-3')
exit(1)
create_gif(sys.argv[1])
8 changes: 4 additions & 4 deletions python/miniwob/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, utterance, fields, dom_info):
"""
################
# Parse utterance
assert isinstance(utterance, basestring)
assert isinstance(utterance, str)
self._phrase = Phrase(utterance)
self._fields = fields
################
Expand Down Expand Up @@ -145,7 +145,7 @@ def __init__(self, raw_dom, parent=None, dom_elements=None):
if self.tag == 't':
self._ref = None # ignore refs for text, since they are unreliable
if 'text' in raw_dom:
self._text = unicode(raw_dom['text'])
self._text = str(raw_dom['text'])
else:
self._text = None
self._value = raw_dom.get('value')
Expand All @@ -168,7 +168,7 @@ def __init__(self, raw_dom, parent=None, dom_elements=None):
# Fix a bug where sometimes children are created even though all children are <t>
# (which will incorrectly make this element a non-leaf and thus unclickable)
if self._children and all(child.tag == 't' for child in self._children):
self._text = u' '.join(child.text for child in self._children)
self._text = ' '.join(child.text for child in self._children)
self._children = []
# Add to the collection
if dom_elements is not None:
Expand Down Expand Up @@ -350,7 +350,7 @@ def visualize(self, join=True):
lines = []
lines.append('- {}'.format(self))
for i, child in enumerate(self.children):
if isinstance(child, unicode):
if isinstance(child, str):
child = child[:20] + '...' if len(child) > 20 else child
lines.append(' |- "{}"'.format(child))
else:
Expand Down
10 changes: 5 additions & 5 deletions python/miniwob/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import regex as re
import re


def strip_punctuation(uni):
Expand All @@ -10,7 +10,7 @@ def strip_punctuation(uni):
Returns:
unicode
"""
return re.sub(ur"\p{P}+", " ", uni)
return re.sub(r"\p{P}+", " ", uni)


def strip_whitespace(uni):
Expand All @@ -22,7 +22,7 @@ def strip_whitespace(uni):
Returns:
unicode
"""
return re.sub(ur"\s+", u"", uni)
return re.sub(r"\s+", "", uni)


def find_sublist(l, sublist):
Expand All @@ -36,7 +36,7 @@ def find_sublist(l, sublist):
Returns
int
"""
for i in xrange(len(l)):
for i in range(len(l)):
# Check index 0 first for optimization
if l[i] == sublist[0] and l[i: i + len(sublist)] == sublist:
return i
Expand All @@ -59,7 +59,7 @@ def __init__(self, text):
Args:
text (str or unicode)
"""
self._text = unicode(text)
self._text = str(text)
self._tokens = None

@property
Expand Down
5 changes: 3 additions & 2 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Pillow>=4.0
selenium>=3.4.3
Pillow
selenium>=4.5.0
pytest
numpy
Loading