Skip to content

Can I use SeleniumBase built-in methods to add elements directly to the page? #1429

Answered by mdmintz
chenqinggang001 asked this question in Q&A
Discussion options

You must be logged in to vote

You'll need to use JavaScript to do that. https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
With SeleniumBase you can call self.execute_script(JS) to execute the JavaScript that you need to create elements.

Eg:

from seleniumbase import BaseCase

class AddButtonTest(BaseCase):
    def test_add_button(self):
        script_to_add_button = """function injectButton() {
           var new_button=document.createElement('button');
           document.getElementsByTagName('body')[0].appendChild(new_button);
           new_button.style="width: 200px; height: 120px";
        }
        injectButton();"""
        self.execute_script(script_to_add_button)
        self.sleep(3)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@chenqinggang001
Comment options

Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants