Selenium(PYTHON) add variable that converts number into syntax - python

So i have a code that changes the text of a class, but instead of manually entering the number, i would like the number to be equal to whatever X is, and convert that number into a string if thats what needs to be done
try:
newButton = WebDriverWait(driver, 100).until(
EC.presence_of_element_located((By.CLASS_NAME, "btn-full")))
finally:
driver.execute_script('arguments[0].innerHTML = "X";', newButton)
driver.execute_script("arguments[0].setAttribute('class','NEWBUTTON')", newButton)
How do i make sure its made in such a way that it takes a variable that holds an integer value, and its written out in place of the X

Well you can pass it as arguments[1] like this:
x = #some number
driver.execute_script('arguments[0].innerHTML = arguments[1];', newButton, x)
If you want it to be an int you can leave it as it is, but if you want it as a string then just convert it wherever you want and pass it into the method.
The arguments[n] is the (n+2)'nd argument you pass into the method execute_script().

Related

Python method positional args troubleshooting

So I am trying to make a script that will traverse a string and replace certain characters. The idea is fairly simple and sudo code looks a little like this.
Input1 = ''
Input2 = ''
Input3 = ''
rawPw = Input1 + Input2 + Input 3
Remove spaces if any exist
Creates a new empty str called finalPw
Scan the rawPw string one character at a time. Each character goes to a Random bool and if True it goes to get converted. If false it appends finalPw
In it goes to conversion it checks against a list of specified characters and if it matches it goes to a specific converter method. Otherwise it will just swap upper/lower case and append finalPw
The specific converter method will use a predefined list of characters that it can be replaced by, and will use random.choice() to pick from that list to replace the character and append finalPw.
This is the code I have so far, note the upper/lower case swapping is not part of it yet, I am building and testing the code piecemeal because my last prototype was 350 lines long and a complete failure. So going from scratch here.
import random
print("Enter 3 words or series of numbers or both, each entry must be at least 5 characters in length")
def main():
input1 = 'Jim'
input2 = 'Samantha'
input3 = 'Ethan'
rawPw = (input1 + input2 + input3)
refinedPw = rawPw.replace(' ', '')
print(refinedPw)
finalPw = ''
convertTrain(refinedPw)
def switch():
switchVal = random.choice(True, False)
return switchVal
def convertTrain():
temp = main.refinedPw
onOff = False
for i in temp:
switch(i)
if i == True:
if i == 'i':
the_iExc(i)
else:
main.finalPw.append(i)
def the_iExc():
rep_iExc = ['i', '!']
repVal = random.choice(rep_iExc)
for i in len(main.refinedwPw):
slice(i)
if i == 'i':
i.replace(repVal)
return i
main()
The error I am receiving is :
line 15, in main
convertTrain(refinedPw)
TypeError: convertTrain() takes 0 positional arguments but 1 was given
I have tried changing things around a bit to see if I can't fix the positional argument, so much so that I have stripped it of all classes and am just going classless until I get it to work. For the life of me I can't seem to get it to use the appropriate number of args.
This line defines convertTrain as a function that takes no arguments:
def convertTrain():
But then inside main() you call it with an argument:
convertTrain(refinedPw)
Either change the function definition to accept an argument, or change the call to not pass an argument.
You're trying to invoke function with arguments here:
convertTrain(refinedPw)
...but there are no arguments in the function's definition
def convertTrain()
To fix this, add a new argument for the convertTrain function, like this:
def convertTrain(train) # Name of argument can be changed if needed
Read more about function declaration here: click this

Python Parameter passing for another function

I have created two functions namely inputData(): and validateNumber():
In the inputData() function I enter a value and stores it in a variable called number. And then I want to pass that parameter to validateNumber(): function. But it isn't work :(
It would be fine if anyone explain me the error :)
Regards.
Here's the code:
def inputData():
number = int(input("Enter a Number: "))
print(number)
return number
def validateNumber(number):
n=2
while number > n:
if number%n==0 and n!=number:
print("Not Prime")
break
else:
print("Prime")
break
return number
inputData()
validateNumber()
You need to perform the function call as follows:
validateNumber(inputData())
or
number = inputData()
validateNumber(number)
with def validateNumber(number) you are telling python that the function validateNumber must receive one parameter when it is called. But, you are not passing the parameter to it when you call it.
If you are new to programming, check this tutorial: Python Functions, to understand:
What are functions
How to define them
How to use them.
You need to store the value of inputData() function in some variable then pass it to second function like this
>> number = inputData()
>> validateNumber(number)
You're not passing the inputted number to the validate function.
returned_input_number = inputData()
validateNumber(returned_input_number)
Also, I find it a bit odd that your validateNumber function returns a number. It might be better to return True or False (depending on if the number is valid or not). Either that, or maybe 'validate' is the wrong name for the function.

How to get input from multiple lines?

i want to take input from user in and each value of the input is on consecutive line.this is to be implemented in python
while x=int(raw_input()): ##<=showing error at this line
print(x)
gollum(x)
#the function gollum() has to be called if the input is present
The reason why your code does not work is why wants a condition or an object. As you are assigning a value (x=raw_input()), while does not find anything to test (an assignment does NOT return any value).
You can either request an input, and then do a while loop depending on the value of this input (that will be modified inside the while loop) :
x = int(raw_input())
while x:
print(x)
gollum(x)
x = int(raw_input())
That gives you an error because x=int(raw_input()) doesn't return a boolean, and you need a boolean inside the while condition.
You can try this one:
while True:
x = raw_input()
if x=='':
break
x = int(x)
print(x)
gollum(x)
that way if you put an empty string (just an enter) the program just stops and doesn't give an annoying error :P

How to complete this function then print it out, using Python?

I'm having a hard time to understand how to work with functions - I can make then but after that I don't know how to use them. My question is how can I print this code with a function?
string = "Hello"
reverse = string[::-1]
print(reverse)
I tried putting it in a function but I cannot make it print Hello.
def reverse_a_string(string):
string = "Hello"
reverse = string[::-1]
print(reverse)
also tried this
def reverse_a_string(string):
string = "Hello"
reverse = string[::-1]
print(reverse)
Nothing seems to work. I'm having same problem with this as well.
total = 0
def length(words):
for i in words:
total += 1
return total
Functions without a return value
Functions that just take action or do something without returning a value (for example, print).
Functions that don't return a value can be defined like that:
def sayHello():
print "Hello!"
And can be used (called) like that:
sayHello()
And the output will be:
Hello!
Function parameters
A function can also receive parameters (type of variables) from the caller. It's better to demonstrate it with an example.
A function that receives a name and greets this name:
def sayHelloTo(name):
print "Hello", name
It can be called like that:
sayHelloTo("Yotam")
And the output will be:
Hello Yotam
The parameters are the function's input.
Functions with a return value
Other functions, unlike sayHello() or sayHelloTo(name) (that just do something) can return a value. For example, let's make a function that rolls a dice (returns a random number between 1 and 6).
from random import randint
def rollDice():
result = randint(1, 6)
return result
The return keyword just sets the output value of the function and exits the function. An example use of the rollDice function will be:
dice = rollDice()
print "The dice says", dice
When the function hits a return keyword, it finishes and the return value (in our case, the variable result) will be placed instead of the function call. Let's assume randint(1, 6) has produced the number 3.
Result becomes 3.
Result is returned.
Now, instead of the line:
dice = rollDice()
We can treat the line as:
dice = 3
(rollDice() was replaced with 3)
Functions with parameters and a return value
Some functions (for example, math functions) can take inputs AND produce outputs. For example, let's make a function that receives 2 numbers and outputs the greater one.
def max(a,b):
if a > b:
return a
else:
return b
What it does is pretty clear, isn't it? If a is greater, it returns the value of it. Otherwise, returns the value of b.
It can be used like that:
print max(4, 6)
And the output will be:
6
Now, your case
What you want to do is a function that reverses a string. It should take 1 parameter (input) - the string you want to reverse, and output 1 value - the reversed string. This can be accomplished like that:
def reverse_a_string(my_text):
return my_text[::-1]
now you can do something like that:
s = raw_input("Please enter a string to be reversed\n") #input in Python3
r = reverse_a_string(s)
print r
r will contain the reversed value of s, and will be printed.
About your second function - well, I assume that based on this answer you can make it yourself, but comment me if you need assistance with the second one.
Local variables
About your 3rd example:
def reverse_a_string(string):
string = "Hello"
reverse = string[::-1]
print(reverse)
This is something that is really worth delaying and understanding.
the variable reverse is first used inside the function. This makes it a local variable.
This means that the variable is stored in the memory when the function is called, and when it finishes, it is removed. You can say it's lifetime is from when the function is called to when the function is done.
This means that even if you called reverse_a_string(string), you wouln't be able to use the reverse variable outside of the function, because it would be local.
If you do want to pass a value like that, you have to "declare" your variable outside of the function and to use the global keyword, like that:
reverse = "" #This makes reverse a global variable
def reverse_a_string(string):
global reverse #Stating that we are going to use the global variable reverse
reverse = string[::-1]
# Then you can call it like that:
reverse_a_string("Hello")
print reverse
The output will be
olleH
Although it's strongly not recommended to do it in Python, or in any other language.
Once you create a function you must call it. You have created the function reverse_a_string but then you never actually call it. Think about a function as a button that does something everytime it is pushed (or in our case called). If you never push the button then although it has the potential to do something, it never will. In order for the set of instructions to happen we need to push the button (or in our case call the function). So in order for your code to work you first need to define the function then actually call it:
def reverse_a_string():
string="Hello"
reverse = string[::-1]
print reverse
reverse_a_string()
Result: 'olleH'
If you want to pass your own string in to the function so it doesn't just return 'olleH' all the time your code needs to look like such:
def reverse_a_string(stringThatWillBeReversed):
reverse = stringThatWillBeReversed[::-1]
print reverse
reverse_a_string('whateverStringYouWant')
Result: The reverse of the string you entered.
Hope that helps!
I don't know whether you are asking how to define functions in python or something else
If you want to learn python functions, go to http://www.tutorialspoint.com/python/python_functions.htm or just write python tutorial in google, you will get billions of good sites
def reverse_a_string(string):
#function definition
reverse = string[::-1]
print(reverse)
#function call
reverse_a_string("your string")
But you to define function for this, you could simply do
print( string[::-1] )
# defines the 'Reverse a String' function and its arguments
def reverse_a_string():
print(string)
reverse = string[::-1]
print(reverse)
print("Type a string") # asks the user for a string input
string = input() # assigns whatever the user input to the string variable
reverse_a_string() # simply calls the function
for functions, you have to define the function, then simply call it with the function name i.e. funtion()
In my example, I ask for a string, assign that to the variable, and use it within the function. If you just want to print hello (I'm a little unclear from your question) then simply including the print("hello") or w/ variable print(string) will work inside the function as well.

How do I access the string inside my function python?

Trying to gain access to this string to test it if it has 3 or more blues "b" inside of it. ---Both test and three_or_more_blues are functions.----- I'm completely lost, any one got an idea? Please change my title if it doesn't fit my question. Not sure really how to ask the question. Thanks!
test(three_or_more_blues, "brrrrrbrrrrrb")
Assuming test is a function that takes a function and a string as paramters, and three_or_more_blues is a function that returns true if its string parameter has 3 or more 'b' characters, then
def test(func, str):
if func(str):
# do something with str
test(three_or_more_blues, "brrrrrbrrrrrb")
You could use .count().
sentence = 'brrrrrbrrrrrb'
amount = sentence.count('b')
print(amount)
And then you could use a loop to calculate your next step.
if (amount >= 3):
# Do something
I am not sure if I understand you correctly - you are asking how to pass the string 'brrrrrbrrrrrb' to the three_or_more_blues function?
If that is the case, than you just simply pass it when you call the three_or_more_blues function like this:
def test(func, some_string):
func(some_string) # here you call the passed function
# if three_or_more_blues would look like this:
def three_or_more_blues(some_string):
print "Yes, 3 or more b's" if some_string.count('b') >= 0 else "No"
# you would get this from your function call
test(three_or_more_blues, "brrrrrbrrrrrb") # prints: "Yes, 3 or more b's"

Categories