Member-only story
Today I will go over some advanced tips for how to interact with elements using javascript and combine selenium with other tools.
I. Work with web elements by JavaScript in Selenium
For more details, you can refer to this javascript-selenium-webdriver
In general there are two ways to call javascript execute from selenium:
Execute script:
element = driver.execute_script(script_string, *args)
element = driver.execute_async_script(script_string,*args)
Notes:
If you want to get element value, your script should have ‘return’ like : ‘return arguments[0].shadowRoot’
- Find element by javascript:
- get element by id:
document.getElementByID('{id}')
- get elements by class name:
document.getElementsByClassName('{className}')
- get elements by selector:
document.querySelector('{selectorName}')
2.Work with Shadow DOM
For more details about Shadow DOM, you can refer to this shadow dowm
In short, they use Shadow DOM to avoid conflicts by different css, interface style