patternpythonMinor
Taking action on a webpage(select checkbox or enter textbox or select from a list)
Viewed 0 times
webpageentercheckboxactionlisttextboxselectfromtaking
Problem
I currently have the following code. This function works successfully, however, i would like some feedback because I think it is very messy.
Currently, this function checks if the field is
If it cannot find any of the above it throws an error that it cannot find the textbox.
I have defined two variables
Here is the function.:
```
def take_action(self, enterValue, checkboxUncheck=False):
try:
child = [
self.text.find_element_by_xpath(PageCommonLocatars.FINDCHECKBOXONE),
self.text.find_element_by_xpath(PageCommonLocatars.FINDCHECKBOXTWO)
]
var = self.text.find_element_by_xpath(PageCommonLocatars.ENTERCHECKBOXVAL % enterValue)
#var = self.text.find_element_by_xpath('./..//..//div/input[@value="%s"]' % enterValue)
if checkboxUncheck == True:
if var.is_selected() == True:
self.driver.execute_script("arguments[0].click();", var)
else:
if var.is_selected() == False:
self.driver.execute_script("arguments[0].click();", var)
except:
try:
Select(self.text.find_element_by_xpath(PageCommonLocatars.FINDSELECT_TYPE)).select_by_visible_text(enterValue)
except:
self.text.find_element_by_xpath(PageCommonLocatars.FINDTEXTBOX)
elemtwo = self.text.find_element_by_xpath(PageCommonLocatars.ENTERTEXTBOXVAL)
actions = webdriver.ActionChains(self.driver)
Currently, this function checks if the field is
checkbox or textbox or Select. Once it verifies the field it performs an action based on that. For example if it is SELECT it will select the value.If it cannot find any of the above it throws an error that it cannot find the textbox.
I have defined two variables
enterValue and checkboxUncheck=False. enterValue is what user wants to enter. checkboxUncheck variable is used so the user can perform two actions, uncheck the checkbox or check it. By default it checks it. However this can be changed if it is set to checkboxUncheck=True. Here is the function.:
```
def take_action(self, enterValue, checkboxUncheck=False):
try:
child = [
self.text.find_element_by_xpath(PageCommonLocatars.FINDCHECKBOXONE),
self.text.find_element_by_xpath(PageCommonLocatars.FINDCHECKBOXTWO)
]
var = self.text.find_element_by_xpath(PageCommonLocatars.ENTERCHECKBOXVAL % enterValue)
#var = self.text.find_element_by_xpath('./..//..//div/input[@value="%s"]' % enterValue)
if checkboxUncheck == True:
if var.is_selected() == True:
self.driver.execute_script("arguments[0].click();", var)
else:
if var.is_selected() == False:
self.driver.execute_script("arguments[0].click();", var)
except:
try:
Select(self.text.find_element_by_xpath(PageCommonLocatars.FINDSELECT_TYPE)).select_by_visible_text(enterValue)
except:
self.text.find_element_by_xpath(PageCommonLocatars.FINDTEXTBOX)
elemtwo = self.text.find_element_by_xpath(PageCommonLocatars.ENTERTEXTBOXVAL)
actions = webdriver.ActionChains(self.driver)
Solution
Multiple things should be fixed:
-
Selenium specific issues:
-
I think you should not actually need the "action chains" here:
and can replace it with just:
Code review and refactoring is a continuous process - if you would apply all the suggested fixes, I bet we'll see more things to improve.
- Stylistic/Code Style issues:
- consistent indentation - per PEP8, use 4 spaces
- follow the variable naming guidelines (PEP8 reference) - in particular, fix the "camel case" names and add underscores between the words in your variable names in upper case
- truthiness checks can be simplified, e.g.
if checkboxUncheckinstead ofcheckboxUncheck == True
- do not use bare except clause - it really makes things less understandable - catch specific exception and let it fail in all other situations
-
Selenium specific issues:
- your XPath expressions are quite fragile - you are going up and down the tree relying on the depth level and HTML markup too much. Think about using meaningful data-oriented
id,classor other attributes to get to the desired elements. And, XPaths are generally less readable and more error-prompt. There are many other location techniques.
- clicking elements via JavaScript might lead to problems, see if you can use regular
click()method (WebDriver click() vs JavaScript click())
- you may benefit from a "Page Element" page object abstraction
-
I think you should not actually need the "action chains" here:
actions = webdriver.ActionChains(self.driver)
actions.move_to_element(elemtwo)
elemtwo.clear()
actions.send_keys(enterValue)
actions.perform()and can replace it with just:
elemtwo.clear()
elemtwo.send_keys(enterValue)Code review and refactoring is a continuous process - if you would apply all the suggested fixes, I bet we'll see more things to improve.
Code Snippets
actions = webdriver.ActionChains(self.driver)
actions.move_to_element(elemtwo)
elemtwo.clear()
actions.send_keys(enterValue)
actions.perform()elemtwo.clear()
elemtwo.send_keys(enterValue)Context
StackExchange Code Review Q#153155, answer score: 2
Revisions (0)
No revisions yet.