2

I need to sellect all checkboxes in webpage using python 3.8 and chromedrive.

This function is working in javascript, i want the same code in python

< script type = "text/javascript" >
    function selects() {
        var ele = document.getElementsByName('chk');
        for (var i = 0; i < ele.length; i++) {
            if (ele[i].type == 'checkbox')
                ele[i].checked = true;
        }
    }
Stefano
  • 21
  • 3

2 Answers2

0

You can pass any JS code to selenium driver, for example:

script = '''
var ele = document.getElementsByName('chk');
for (var i = 0; i < ele.length; i++) {
    if (ele[i].type == 'checkbox')
        ele[i].checked = true;
}'''

diver.get("https://somedomain.com/")
driver.execute_script(script)
akdev
  • 586
  • 2
  • 6
  • 18
0

Thanks william wu, your answer helped! find_element_by_* commands are deprecated in python 3.7+ I did some modifications


xPath = "//input[@type='checkbox']"
elems = driver.find_elements("xpath", xPath)
for elem in elems:
    if elem.tag_name == "input" and elem.get_attribute("type") == "checkbox":
        elem.click()


This code worked for me and thanks again.

Stefano
  • 21
  • 3