I am trying understand why an if else isn't working using Selenium and Python. I have tried several different variations, but I believe this should work and stubbornly, maybe blindly, keep coming back to it.
example:
if (self.driver.find_element_by_class_name('product')).text == "FEATURED PRODUCTS":
print "\nHome Pass"
else:
print "\nHome Fail"
Tried this as well
if self.driver.find_element_by_class_name("product-teaser-list").text in "Featured Products":
Each execution returns Fail and when using assert, below, the .text is found. So I know the text is found with this method.
assert "FEATURED PRODUCTS" in self.driver.find_element_by_class_name('product').text
try this following code.
if "FEATURED PRODUCTS" in self.driver.find_element_by_class_name('product').text
If this isn't working try to debug that. something white space creates an issue. however given the assert statement is passing, following should work.
Related
i am trying to execute this very short code that took out from my bigger code because i was having problems with it. I was able to reproduce the problem (it gave the exact same error).
BACKGROUND INFO: i am just trying to get the first sentence out of this wikipedia search. but because the word i am searching for (kip, means chicken in Dutch) has a wide variety of meanings or something i get an error. i want to bypass this error using try: except: but it keeps displaying the error message anyway.
here is the code that just doesnt seem to work:
import wikipedia
wikipedia.set_lang('nl')
try:
summry = wikipedia.summary('kip', sentences=1)
print(summry + "\n")
except:
print("error")
i have tried replacing except: with this
except wikipedia.exceptions.DisambiguationError:
but it still doesnt work :( it always displays the error code regradless and prints "error" afterwards
/opt/virtualenvs/python3/lib/python3.8/site-packages/wikipedia/wikipedia.py:389:
GuessedAtParserWarning: No parser was explicitly specified, so I'm using the best available HTML
parser for this system ("html5lib"). This usually isn't a problem, but if you run this code on
another system, or in a different virtual environment, it may use a different parser and behave
differently.
The code that caused this warning is on line 389 of the file
/opt/virtualenvs/python3/lib/python3.8/site-packages/wikipedia/wikipedia.py. To get rid of this
warning, pass the additional argument 'features="html5lib"' to the BeautifulSoup constructor.
lis = BeautifulSoup(html).find_all('li')
error
i am using repl.it to program this
if anyone has any idea about why it keeps displaying the error anyway please please let me know :D
First of all, thank you all for commenting :D it helped a lot
At the end the solution that worked for me was inspired from Askold Ilvento's comment
although
with warnings.catch_warnings(): warnings.simplefilter("ignore")
didn't work when i adapted it a little bit it did the job!
this is the code that solved the problem and allowed me to ignore what was actually a warning (not an exception) and stop displaying it
import warnings
warnings.catch_warnings()
warnings.simplefilter("ignore")
i just added this at the start of the script and it solved the problem :D
once again all the credit for this code goes to Askold Ilvento i just had to add a little bit to it to make it work
So I'm pretty new to both coding and this website, so please bear with me if this is stupid:
I'm working on a personal project and would like to find a way to clear "print()" statements in python 3.6. For example:
print("The user would see this text.")
but if I continue
print("The user would see this text.")
print("They would also see this text.")
Is there a way to make it so a user would only see the second print statement?
I have seen "os.system('cls')" and "os.system('clear')" recommended, but I get these errors for each:
os.system('cls')
resulting in
sh: 1: cls: not found
and
os.system('clear')
resulting in
TERM environment variable not set.
Obviously I'm missing something, but if you know what it'd be much appreciated. If you know of another way to do what I'm thinking, that would also be awesome. Thank you for taking the time to read this, and thanks for any help.
Edit: I'm using Repl.it as my IDE. Could this be an issue with that site specifically?
Edit: Downloaded a new IDE to check, and the reply worked. If you are new and using Repl.it, be aware that some code does not function properly.
The method that I've used in the past to 'reprint' something on an existing line is to make use of the standard output directly, coupled with a carriage return to bring the printed statement's cursor back to the start of the line (\r = carriage return), instead of relying on the print function.
In pseudocode:
# Send what you want to print initially to standard output, with a carriage return appended to the front of it.
# Flush the contents of standard output.
# Send the second thing you want to print to standard output.
A working example in Python:
import sys
sys.stdout.write('\rThe user would see this text')
sys.stdout.flush()
sys.stdout.write('\rThe user would also see this text')
Edit
Figured I'd add an example where you can actually see the code working, since the working example above is going to execute so quickly that you'll never see the original line. The below code incorporates a sleep so that you can see it print the first line, wait, then reprint the line using the second string:
import sys
from time import sleep
sys.stdout.write('\rThe user would see this text')
sys.stdout.flush()
sleep(2)
sys.stdout.write('\rThe user would also see this text')
Hi i've been trying to learn selenium.
The problem i am having is that an element isn't being detected. The html code i am working with is highlighted right here https://gyazo.com/a76608d4c4e9fc3bd29412436604c173. That line of code stopped my program and gave me the error https://gyazo.com/72fc72e6488578c9c2a50b71311602c5.
The code i am using to detect for the textarea class is
comment=driver.find_element_by_xpath("//form[#class='comment-form no-border-top']/div/*[1]") or driver.find_element_by_xpath("//form[#class='comment-form']/div/*[1]")
because sometimes it switches between comment-form no border and comment form.
My idea is to use a conditional assignment operator check for example if(comment!=none): do something, but to my understanding python doesn't allow this.
any suggestions on what to do and whether my xpath is a reliable method?
If the problem you're trying to solve is actually conditional assignment, there are two different things you might want to know.
The first is that you can do:
if(comment!=none)
you just have to do it like this:
comment = Foo()
if comment is None:
comment = Bar()
Second, maybe you want to try assigning comment if the first call throws an error rather than just being None. If so, try using try!
from selenium.common.exceptions import NoSuchElementException
try:
comment = driver.find_element_by_xpath("//form[#class='comment-form no-border-top']/div/*[1]")
except NoSuchElementException:
comment = driver.find_element_by_xpath("//form[#class='comment-form']/div/*[1]")
try:
driver = launch_browser()
except:
print "Browser launch failed"
driver.get("http://www.example.com/")
The last line above is flagged by PyCharm with the following issue:
Local variable "driver" might be referenced before assignment
However, something like this makes the error go away:
driver = None
try:
driver = launch_browser()
except:
print "Browser launch failed"
driver.get("http://www.example.com/")
Is there a way to setup PyCharm so that it will see the assignements inside try blocks?
Secondarily, can PyCharm figure out the type based on the return value of the function (in this case launch_browser()) if it has docstrings?
BTW, code works just fine in both cases. It's just a matter of getting PyCharm to understand the assignment inside the try block without having to resort to a band-aid.
EDIT 1:
A return in the except: block fixes the problem as far as PyCharm is concerned. I was working on something else and inadvertently commented it out. Proof that coding for 16 hours straight is a really bad idea...
If launch_browser() fails, your code will error at the driver.get("http://www.example.com/") line. PyCharm is letting you know this.
The only way to avoid this is by not executing anything below the except, e.g. throwing an exception inside it, or putting everything that relies on driver inside an else block, which will only run if no exception is caught. E.g.
try:
driver = launch_browser()
except:
print "Browser launch failed"
else:
driver.get("http://www.example.com/")
I'm having a syntax error on the second line of this code, I'm trying to make a counter with the winsound beep.
I think the problem is with the format() part, but i get a highlighted =, equals sign when i try to run the program. syntax error
def print_time(secs):
print('{0}:{1:02}'.format(secs//60,secs%60),end=' ')
print("left to wait...")
This is my second week programming, very basic understanding of comp sci or of languages.
This looks like a wonderful site to learn from.
If the part of the code I wrote looks fine, i can post the rest of it up as well to help find the problem.
It sounds like you're reading documentation for Python 3.x, but running Python 2.x. Try this instead:
def print_time(secs):
print '{0}:{1:02}'.format(secs//60,secs%60),
print "left to wait..."
Also, divmod().
def print_time(secs):
print '{0}:{1:02}'.format(secs//60,secs%60),
print "left to wait..."
The above code should work fine.
Python 3+ treats 'print' as a function and hence introduces end=' ' to suppress newline. But, in earlier versions of python it was done by appending ,(comma) to the print statement. See this link for what's new in Python 3+.
Apparently, your Python environment is 2.x and hence you are seeing the error.