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;
}
}
CodePudding user response:
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)
CodePudding user response:
Here is an example of how to select all checkboxes in a webpage using Python 3.8 and ChromeDrive:
from selenium import webdriver
# Start the Chrome browser
browser = webdriver.Chrome()
# Visit the webpage
browser.get('http://www.example.com')
# Get all elements with the name attribute set to 'chk'
elems = browser.find_elements_by_name('chk')
# Iterate over the elements and check the checkbox if it is one
for elem in elems:
if elem.tag_name == 'input' and elem.get_attribute('type') == 'checkbox':
elem.click()
The code above starts the Chrome browser and visits the specified webpage. Then, it uses the find_elements_by_name method to get all elements with the name attribute set to 'chk', and iterates over the elements, checking the checkbox if it is one.
Note that the code above is just an example, and may need to be adjusted to fit your specific needs.
CodePudding user response:
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.