browser.find_element_by_xpath('//div[contains(text(), "SomeData")]')
I want to pass a variable instead of a string, I am using Selenuim with python. In this Case A div will be found with text of "SomeData" everytime instead of passing a value as string I want it a variable, which will change everytime I run the code.
I cannot give a correct answer as I am unable to fully understand your question. But lets assume, you want to have some part of your stated xpath as a variable, that part can be stored in a variable and then referenced in your function call.
And my_variable can be controlled in your code and change value as needed without writing complete xpath
Again having said that, more clarity on your question will help the community help you bette
my_variable = 'SomeData'
browser.find_element_by_xpath(f'//div[contains(text(), "{my_variable}")]')
if you are using python version less that 3.6
my_variable = 'SomeData'
browser.find_element_by_xpath(f'//div[contains(text(), "{my_variable}")]'.format(my_variable=my_variable))
or syntax provided in the comment by Andersson
Related
I'm new to JMeter so this question may sound absolutely dumb...
I have a loop in which a variable (let's say it is called "raw") is being changed and written to file every iteration. The variable contains HTML encoded text so it has to be converted into plain text. I found out this can be done using __unescapeHtml function. When I tried using it worked but I ended up always receiving the same text as on the first iteration. Then I learned that I have to use vars.get instead of ${} to access a variable. So I changed ${__unescapeHtml("${raw}")} to ${__unescapeHtml(vars.get("raw")} which kind of helped: vars.get is getting the new value of raw each iteration but __unescapeHtml didn't work at all now - it just returns the encoded text from raw. I didn't succeded finding anything about this exact problem so I'm kind of stuck.
Ended up using
import org.apache.commons.lang3.StringEscapeUtils
...
StringEscapeUtils.unescapeHtml4(vars.get("raw"))
Don't know if it is a good way to do this but at least it works.
I assume, that you are using the expression ${...} inside a JSR-223 sampler or similar context. The user manual for JSR-223 Sampler states, that those scripts can be cached by JMeter. That is why you only get the values from the first time the context gets created.
The same is true for simple variable evaluations as ${varname}, as for function calls like ${__unescapeHtml(...)}.
The solution here is:
don't use ${...} inside of JSR-223 contexts, that might be cached.
you can however pass those expressions (${...}) into the context by using them as parameters through the input labeled Parameters on the JSR-223 Sampler – again assuming, that you are using it.
you can use the features, that your chosen JSR-223 context gives you, as you have done, by using the StringEscapeUtils#unescapeHtml4
I am a beginner in python told my friend today, that the following code would throw an error, but it did not:
a = 5
a
So I wondered, what does "a" actually do and why is the interpreter fine with this?
If this is a duplicate, please refer me to the right post and sorry in advance.
edit: I used a *.py file.
If you type this code into the shell and click enter, the value of a is returned. Functionally, as there is no operation being performed on a, the value of a will not change.
You define the variable in the line above. The variable contains a value, so the "NameError: name 'a' is not defined" error is not triggered.
Also, even if the variable is a different data type, for example, a string, the value of a is returned.
If you run the code in a different environment, the line won't be printed and the line won't impact the value of itself or of any other variables.
I think you tried it in der REPL Console, paste it to a *.py file and execute that. So when you just type the variable name and hit enter this is actually a print command behind the scenes
You can type in a int into the shell or whatever, and it will return it. The variable you put is just a int, so it returns 5.
I want to create a function that takes some chain of characters as an argument, and uses it as a str object.
def useless_function(argument) :
print(argument)
useless_function(banana)
--> NameError: name 'banana' is not defined
So this is what I did : I created a decorator that turns whatever I enter as argument into a str my function can print.
def decorator(f) :
def wrapper(arg_f) :
str_arg = str(arg)
f(str_arg)
return wrapper
So now I can decorate useless_function with my decorator, and useless_function(banana) will print 'banana'. And it will work with whatever it enter as an argument of useless_function.
My question is : is there a more elegant way or a simpler and faster way to do this automatic transformation into a string that can be used as an argument ?
Can you please elaborate because I don't understand what it is that you are looking for or saying.
If you mean: inside a function can you do input("variable")? Then the answer is yes. It is just essentially raw_input() from python2. The input from your keyboard will always be a str if I am not mistaken.
Update after edited post:
It is still not any more clear what you are trying to do.
At the end of the function, you do return * but I assume you know this.
I am really confused, but have you considered just doing str(argument)? As in takes_argument(str(argument))
2nd Update after 2nd edit:
I think I finally understand what you are trying to do, but I might be wrong.
Now, the problem is that def useless_function(argument) : will expect argument to be defined as a variable with some value(s). I am not aware of any other way than actually putting "argument" to tell python that what you are inserting is a string of characters rather than trying to reference some variable and its value. It is the same case as with print('something'), if I were to put print(something), python would try to look up the variable called something which you haven't defined.
Hope that makes sense.
I'm having trouble with the input() function in Python3.4 using the Anaconda integrated editor. If I just type
x = input()
into the editor, it returns a blank line that I can type text into. If I type:
foo
into this line, I would expect 'foo' be stored as a string with variable name x. But, instead I get:
NameError: name 'foo' is not defined
To make the function work as expected, I must instead type in:
'foo'
which is unfortunate because what I really want is just to pause my code and wait for an arbitrary user input, and I read somewhere that "wait = input()" is the most pythonic way to do this. Using that line in my actual script returns an "unexpected EOF" error - I assume as another symptom of the same problem. Can anyone suggest a workaround?
Note: I suspect this is an Anaconda-specific problem, given the following reference:
https://docs.python.org/3.4/library/functions.html#input
Thanks for your time.
Your code is being run by Python 2, not 3. I don't know enough about Anaconda to know if the problem is with their editor, or if you have your path messed up, but the problem is that the wrong version of Python is being used.
I am using a simple looping plugin so that my template looks like this:
{exp:loop_plus start="1" end="4" increment="1"}
<h3>{slide_{index}_title}</h3>
{/exp:loop_plus}
However, I am ending up with the following output:
<h3>{slide_1_title}</h3>
<h3>{slide_2_title}</h3>
<h3>{slide_3_title}</h3>
<h3>{slide_4_title}</h3>
Is there any way I can have dynamic variable names like this? I am not looking for alternative methods for building a slider, I simply would like to know if the dynamic variable names like this is possible. Thanks!
I'm assuming that Loop Plus (http://devot-ee.com/add-ons/loop-plus) sets the {index} part, so the question is what is defining {slide_1_title}...?
Assuming you have an entry field or variable with this defined, what you have is correct, but if it's not working, it means there's a parsing order issue.
Let's assume the code you supplied is wrapped in a {exp:channel:entries} tag pair, what happens is EE will try to parse the variable first, so will see: {slide_{index}_title} which doesn't exist. The {exp:loop_plus} add-on will then parse it, converting it to {slide_1_title} (but to late as channel:entries has already tried to parse it), which is what is finally output to the template.
So what you want to ensure is that EE parses {exp:loop_plus} before {exp:channel:entries}, do this using parse="inward" tag:
{exp:loop_plus start="1" end="4" increment="1" parse="inward"}
<h3>{slide_{index}_title}</h3>
{/exp:loop_plus}
This is a global EE parameter that EE uses to control parse order - you won't find it documented under the specific add-on. By adding the parameter, it means this child tag will get parsed before it's parent.
One way you could do it is to declare a preload_replace variable in your template and use it in your custom field name.
So something like:
{preload_replace:my_var_prefix="whatever"}
And then in your loop, you could then use:
{slide_{my_var_prefix}_title}