How to change css by class name in selenium

I am learning how to use Selenium in pythonand I am trying to change the style cssto http://www.google.com . For example, <span class="gbts"> ....</span>on this page. I would like to change the class gbts.

browser.execute_script("q = document.getElementById('gbts');" + "q.style.border = '1px solid red';")

Is there an API method called getElementByClass('gbts')?

+4
source share
2 answers

You are asking how to get a CSS class element using JavaScript. Nothing to do with Selenium really.

Despite this, you have several options. You can grab an element first using Selenium (and so, yes, Selenium matters):

element = driver.find_element_by_class_name("gbts")

, :

driver.execute_script("arguments[0].style.border = '1px solid red';")

( , arguments[0])

JavaScript JavaScript , . , JavaScript getElementByClassName. getElementsByClassName, , , .

, , , . , gbts, :

driver.execute_script("document.getElementsByClassName('gbts')[0].style.border = '1px solid red';")

, , .

+4

, , - document.querySelector().

, , , , , , document.querySelector("#parent-div .gbts").

. ( Mozilla).

+1

All Articles