I am writing a script in Python using Selenium that auto fills out a web form (a helpdesk ticketing system)
A factor in this is the body of the ticket does not have an element id that Selenium recognizes, so in order to type in the body I have to find the title element, press the tab key, then start typing in to the body.
Here is some code that writes a message into the body:
der = "/t this is the desc"
driver.find_element_by_id("title").send_keys(der)
The problem is, this code doesnt work for me. What I really need to do would look like this:
body = open(email.txt)
driver.find_element_by_id("title").send_keys("/t" + body)
So I want it to find the title element, press the tab key, then write what is stored in the body variable into the body of the ticket. the only issue is that syntax is bad.
I looked at SendKeys but that is windows only. I am using Fedora 16.
Any help/recommendations would be greatly appreciated.
Thanks!
You have a bug in your code. Change this:
body = open(email.txt)
to:
body = open("email.txt").read()
Related
I know that questions like this have already been asked but I have not found a clear answer that really works.
I want to send automatically dm on social medias but I want to add emojis in it. The thing is that I don't understand how do people send it because it is not allowed by the Chromedriver (it is said that only BMP is supported).
Indeed, I have found a solution to add text in an input, which is as follows :
JS_ADD_TEXT_TO_INPUT = """
var elm = arguments[0], txt = arguments[1];
elm.value += txt;
elm.dispatchEvent(new Event('change'));
"""
In my case, the place where i want to add emoji is not always an input (it can be a span or a div). Does someone have an idea about what code could help me to do that ? Thanks in advance !
If you're trying to add an emoji to a non-input field, you could set the textContent directly to it.
script = """document.querySelector('%s').textContent='%s';""" % (
css_selector,
value,
)
driver.execute_script(script)
Be sure to escape any quotes & special characters before feeding a selector in there. Eg. import re; re.escape(css_selector)
That will let you set any text on a web page element to anything, including emojis if you're not typing into an input field.
How to automatically remove the m. from the URL, while keeping the rest of the current link in full, in order to switch from mobile mode to computer mode in Selenium by Python 3.8.2
https://m.facebook.com/*************
I have tried this code but failed
driver.get('https://m.facebook.com/***********').replace("https://m.", "")
You can do this by just using the replace() function of the str class:
link = 'https://m.facebook.com/whatever'
link = link.replace('/m.facebook', '/facebook')
EDIT: After seeing your updated answer, what you were doing wrong is that you were trying to call the replace() function on get(). You need to call it directly after the string.
driver.get('https://m.facebook.com/***********'.replace("https://m.", ""))
Recently i started working on a project. In which i am sending data from a html page using <a> tag. At backend i am using python webapp2 feamework. When i get the data it show perfectly. But when i compare it with some string for further usage it does not work.
I know when we get data it is in unicode. But i converted it to utf-8 and it is still not working.
Here is the code in html. Suppose i sent "item 2" as itemname
Click me
The code which i am using to fetch data is
def get(self,nam,des):
nam = self.request.get('itemname')
itemDesc= self.request.get('itemdescription')
name = nam.encode('utf-8')
if name == "item 2":
self.response.write("Equal")
I also try it without encoding but still it not works. It Show the value of item name perfectly. But it is not comparing them. Please help where i am doing mistake.
Looks like 2 issues, there is a space before the expected value is set, and the string is urlquoted.
To fix the space:
Click me
And then to work with the encoding, add import urllib and change the line
name = nam.encode('utf-8')
to
name = urllib.unquote(nam.encode('utf-8'))
I've been experimenting with a Python CGI script to send an e-mail (hosted with a comercial web host - 123reg), and the problem is whenever I run the script from my web browser, it sends two identical e-mails.
The code to send the mail is definitely only being executed once, there are no loops which could cause it to happen twice, I am definitely not clicking the button twice. No exceptions are thrown and the "success" page is sent to the browser as normal.
The strangest thing is that when I comment out the code to print the result page (which is very simple and has no side effects, just 3 print statements in a row) and replace it with a dummy print statement (print "Content-type: text/plain\n\ntest"), it works properly and only sends one e-mail.
I have tried googling the problem to no avail.
I am at my wit's end because this problem doesn't make any sense to me. I'm pretty sure it must be my script since inexplicably it works when you comment out those print statements.
I'd appreciate any help, thanks.
EDIT:
Here's the code which, when commented out, fixes the problem:
print "Content-type: text/html"
print
print page
EDIT:
The code to send the e-mail:
#send_email function: sends message from from_addr, assumes valid input
def send_email(from_addr, message):
#form the email headers/text:
email = "From: " + from_addr + "\n"
email += "To: " + TO[0] + "\n"
email += "Subject: " + SUBJECT + "\n"
email += "\n"
email += message
#return true for success, false for failure:
try:
server = smtplib.SMTP(SERVER)
server.sendmail(from_addr, TO, email)
server.quit()
return True;
except smtplib.SMTPException:
return False;
#end of send_email function
I'd post the code to format the page variable, but all it does is read from a file, format a string and return the string. Nothing unusual going on.
EDIT
OK, I've commented out the file IO code in the create_page function and it solves the issue, but I don't understand why, and I don't know how to modify it so that it'll work properly.
The create_page function, and therefore the file IO, was still being executed when I found that commenting out the print statements solved the problem.
This is the file IO code from before I commented it out (it's at the very start of the create_page function and the rest of the function simply modifies the page string, then returns it):
#read the template from the file:
frame_f = open(FRAME)
page = frame_f.read()
frame_f.close()
EDIT:
I have just replaced the file IO by copying and pasting the file text directly into a string in my source file, so there is no longer any file IO. This still hasn't fixed the problem. At this point my only theory is that computers hate me...
EDIT:
I'll have to post this here since stackoverflow won't let me answer my own question since I'm a newbie here...
EDIT:
OK, I posted it as an actual answer now.
PROBLEM SOLVED!
It turns out that it was the browser's fault all along. The reason I didn't notice this sooner was because I tested it in both Firefox and Chrome ages ago to rule the browser out, however it turns out that both Chrome and Firefox share this same bug.
I realised what was happening when the server logs finally updated, I realised that often GET requests were immediately (1 second later) followed by another GET request. I did some googling and found this:
What causes Firefox to make a GET request after submitting a form via the POST method?
It turns out that if you have an img tag with an empty src attribute e.g.
<img src=""/>
(I had some javascript which modified that tag), Firefox will send a duplicate GET request in place of a request for the image. It also turns out that Chrome has the same problem. This also explains why the problem was only happening when I was trying to include my html template.
It would help if you posted more code, but does the "page" variable contain code that would execute the email server a second time, or cause a page refresh that would trigger the email a second time.
The same thing will happen if you have a Javascript call with an empty src or "#" as src:
<script type="text/javascript" src="#"></script>
Perhaps also with an empty href for a css link. I haven't experienced that, but I'd expect the same behavior.
I have a CGI script for which I've successfully set a cookie (which I can see in Firefox/Chrome!) which has (say) the name uid and the content 1. I don't seem to understand how to access this cookie from another CGI script--and I'm working in Python 2.4 so a lot of the examples I've found may not apply.
This code prints "can't get uid" followed by the rest of the page:
c = Cookie.SimpleCookie(os.environ.get("HTTP_COOKIE"))
print("Content-Type: text/html")
print c.output()
print("\n\n")
uid = c.get("uid")
#uid = c["uid"].value # this would create an error and page would fail totally
if uid is None:
print("can't get uid")
uid = 1 # set manually to prevent the rest of the page from failing
I haven't done anything fishy with the domain the cookie applies to, so I don't understand why this doesn't grab the uid value. By the way, if I try to print c.output(), it's blank.
First thing is are you sure the webserver or the framework is setting the HTTP_COOKIE environment variable?
Otherwise, in one of your script you may want to store the cookies in the CookieJar file in the file system and access the set cookies from there.
import cookielib
COOKIEFILE = 'Cookies.lwp'
cookiejar = cookielib.LWPCookieJar()
cookiejar.load(COOKIEFILE)
cookiejar["uid"] = 1
cookiejar.save(COOKIEFILE)
Load the same cookiejar and do the get of uid in the other script.
Okay, I think I figured this out! I confirmed that os.environ.get("HTTP_COOKIE") was getting something, and then played with the order of the elements in my tiny test until it worked. Then I reproduced that order in my more complicated script. (Specifically: content type declaration, two newlines, get cookie, get value from cookie, everything else.)
The main thing I've learned about Python and CGI is that the order of elements (starting with the content type declaration) is very fussy. Thanks very much for the hints in the right direction.