0

The goal

I am working on a private project, written in Python3 and using Selenium to write information from a local database into input-elements on an HTML file of a website.

More explicitly : I want to write to a dnd-character-sheet-html on the website roll20 (https://roll20.org).

The Problem

Sadly, the "send_keys()" method of the selenium package often drops characters, due to a known bugg in the drivers for firefox and chrome that selenium uses. Thus I want to write using javascript directly. I managed to write to some elements, however, the second I click anywhere on the HTML all the entered values disappear.

Current Solution Attempts

Currently I identify the element I want to write to through its xpath and then set its "value" attribute. Below is some example code for this:

import selenium


class DemoClass:
    def __init__(self, driver):
        self.driver = driver
        self.known_element_xpaths = ['Some/Xpath1', 'Some/Xpath2', 'Some/Xpath3'] 

    def write_all_elements(self, input_string):
        for element_xpath in self.known_element_xpaths:
            self.write_to_element(element_xpath, input_string)

    def write_to_element(self, element_xpath, input_string):
        js_command = f'document.evaluate(\'{element_xpath}\', document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.value = \'{input_string}\';'
        self.driver.execute_script(js_command)

driver = selenium.webdriver.chrome()
page = DemoClass(driver)
page.write_all_elements('Test')

I do not know enough about javascript to understand why values keep disappearing. My last searches showed me that it might be about the form (?) being updated with its default values after clicking, leading to a reset of the HTML (?).

Either way, it seems to me I should've used a different way in javascript to write to these elements. What is the correct way to do it here?

Any help or pointing towards the right solution would be appreciated, I am a fair bit out of my depth here.

Attached Resources

In case it helps, here is also a screenshot of the section where I am trying to write to elements and a more close-up screenshot of the exact elements I'm trying to write to. Further here is an example HTML (including associated resources), but please note the text only disappears when writing to- and clicking on a character sheet online.

The spell-section of a character sheet



The spell elements I want to write to

Philipp Doerner
  • 1,090
  • 7
  • 24

1 Answers1

0

You could try setting

driver.implicitly_wait(10)

either when declaring the driver or in the class constructor

self.driver = driver
self.driver.implicitly_wait(10)

Explanation here


If you want to avoid using JS (because its very hard to debug using selenium and python - been there), you could checkout this post on selenium-not-setting-input-field-value. Note the .clear() method that is used before sending keys.

The post you mention about the driver bug also has this answer, have you tried that?

Did you tried and press Enter (programmatically or manually) after sending the keys. Maybe the site expects some kind of field validation.

Bastien Harkins
  • 295
  • 1
  • 7