I'd prefer to refrain from using xpaths unless it is indeed the only way to do it. Here's the simple checkbox I'm playing with: w3schools.checkbox
I want to check the "I have a bike". I tried calling the find_element_by_name method on self.driver but wihtout success and here's my miserable attempt to use xpaths:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
class CheckBox:
def __init__(self):
self.url = 'https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_checked'
self.driver = webdriver.Firefox()
self.driver.get(self.url)
def check_I_have_a_bike(self):
bike_xpath = ".//input[@value='Bike']" # seems pretty straightforward and simple
try:
self.driver.find_element_by_xpath(bike_xpath).click()
except NoSuchElementException as e:
print('Error: {error_message}'.format(error_message=e))
checker = CheckBox()
checker.check_I_have_a_bike()
`Error: Unable to locate element: .//input[@value='Bike']`
What do I do wrong?