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.
Related
I have built a web crawler for a forum game in which players use specific keywords in [b] bold [/b] tags to issue their commands. The bot's job is to traverse through the thread and keep a record of all player's commands, however I'm running into a problem where if player A quotes a post from player B, the bot reads the command of player B in the quote and updates the table for player A.
I have found the specific class name of the quote box, but I cannot figure out how to remove the class from the entire post body.
I tried converting the post to text using the get_attribute('innerHTML') and successfully removed it using regex, however the code I wrote to extract the bold tags (find_attribute_by_tag_name) becomes invalid.
I have two questions for the geniuses that post here:
Is there a way I can delete a specific element from the post body? I searched throughout google and could not find a working solution
Otherwise, is there a way I can convert the HTML I get from get_attribute('innerHTML') back to an element?
def ScrapPosts( driver ):
posts=driver.find_elements_by_class_name("postdetails")
print("Total number of posts on this page:", len(posts))
for post in posts:
#print("username:",post.find_element_by_tag_name("Strong").text)
username=post.find_element_by_tag_name("Strong").text.upper()
#remove the quote boxes before sending to check command?
post_txt=post.find_element_by_class_name("content")
CheckCommand(post_txt, username)
Selenium doesn't have a built in method for deleting elements. However, you can execute some javascript code that can remove the quote box elements. See related question at: https://stackoverflow.com/a/22519967/7880461
This code will delete all elements with the class name quoteBox which I think would work for you if you just change the class name.
driver.execute_script('''
var element = document.getElementsByClassName("quoteBox"), index;
for (index = element.length - 1; index >= 0; index--) {
element[index].parentNode.removeChild(element[index]);
}
''')
Same answer- no built in way of doing that but you can use javascript. This approach would probably a lot more complicated than the first one.
I want to know how I can write a message on linkedin to send my connections. I've done using send keys but it just write only one line I also have used Keys.Shift + keys.enter but it can only write two lines. is there any any way to write a full message in multiple lines using selenium.. to writing a new line it is also doesn't work "\n"
Please let me know is there any way....
I have tried below code:-
description = browser.find_element_by_id("compose-message")
description .send_keys('Hi' , Keys.SHIFT + Keys.ENTER, 'How are you')
description .send_keys('Hi' "\n" 'How are you "\n" it is me')
both don't work to write multiple lines
This worked for me...
description = browser.find_element_by_id("compose-message")
description.send_keys("Hi\nHow are you\n it is me")
I'm not sure what the page looks like and there is a typo above you have a space between description and send_keys e.g.
description .send_keys('')
should be
description.send_keys('')
I also don't understand why you what to use shift enter (perhaps to add a new line?) If so have you tried
description.send_keys('Hi,\nHow are you')
Where '\n' is a newline delimiter
There is just need to uncheck the "press enter to send" in the message box nothing to do anything it works.. One last thing is. it doesn't work on Firefox when sending a long message just use Chrome webdriver
With Flask-wtf I want to display an error message, which should contain a HTML link to the existing profile. I need to convert the built link as string to HTML, which will be appended to the error message.
Here is an excerpt from the code:
class AddBookmarkForm(Form):
# Some Python code to define the form
def validate_bookmark_url(self, field):
if the bookmark_url exists:
bookmark_id = fetching the bookmarks id
link = 'Show bookmark?'
raise ValidationError('User already exists. ' + link)
The ouput is just a string like 'User already saved. <a href="bookmark/123456'>Show bookmark?</a>'.
How do I convert this to executable HTML inside the Python script?
I would make a few suggestions here even though I am not directly answering your exact question. The reason is that I think you should not do it the way you are trying to:
1) If a user already exists, then why do you even need to show them the URL ? Just say "This user already exists". The user who exists should already know their URL. Also, I am assuming that the url /userid needs a login first.
2) But lets say that you still want to tell the user to login, then I would rather change the code to this:
if the user_exists:
return redirect(url_for(login)) # assuming you have a view function for login.
This way, you are just asking them to login which anyway is what the end goal is in case user exists already.
This in my opinion will be a better way to do it. You don't need to worry about passing the URL in error message string. Error messages should just be simple strings as a good practice. No need to complicate them.
I am trying to migrate a forum to phpbb3 with python/xpath. Although I am pretty new to python and xpath, it is going well. However, I need help with an error.
(The source file has been downloaded and processed with tagsoup.)
Firefox/Firebug show xpath: /html/body/table[5]/tbody/tr[position()>1]/td/a[3]/b
(in my script without tbody)
Here is an abbreviated version of my code:
forumfile="morethread-alte-korken-fruchtweinkeller-89069-6046822-0.html"
XPOSTS = "/html/body/table[5]/tr[position()>1]"
t = etree.parse(forumfile)
allposts = t.xpath(XPOSTS)
XUSER = "td[1]/a[3]/b"
XREG = "td/span"
XTIME = "td[2]/table/tr/td[1]/span"
XTEXT = "td[2]/p"
XSIG = "td[2]/i"
XAVAT = "td/img[last()]"
XPOSTITEL = "/html/body/table[3]/tr/td/table/tr/td/div/h3"
XSUBF = "/html/body/table[3]/tr/td/table/tr/td/div/strong[position()=1]"
for p in allposts:
unreg=0
username = None
username = p.find(XUSER).text #this is where it goes haywire
When the loop hits user "tompson" / position()=11 at the end of the file, I get
AttributeError: 'NoneType' object has no attribute 'text'
I've tried a lot of try except else finallys, but they weren't helpful.
I am getting much more information later in the script such as date of post, date of user registry, the url and attributes of the avatar, the content of the post...
The script works for hundreds of other files/sites of this forum.
This is no encode/decode problem. And it is not "limited" to the XUSER part. I tried to "hardcode" the username, then the date of registry will fail. If I skip those, the text of the post (code see below) will fail...
#text of getpost
text = etree.tostring(p.find(XTEXT),pretty_print=True)
Now, this whole error would make sense if my xpath would be wrong. However, all the other files and the first numbers of users in this file work. it is only this "one" at position()=11
Is position() uncapable of going >10 ? I don't think so?
Am I missing something?
Question answered!
I have found the answer...
I must have been very tired when I tried to fix it and came here to ask for help. I did not see something quite obvious...
The way I posted my problem, it was not visible either.
the HTML I downloaded and processed with tagsoup had an additional tag at position 11... this was not visible on the website and screwed with my xpath
(It probably is crappy html generated by the forum in combination with tagsoups attempt to make it parseable)
out of >20000 files less than 20 are afflicted, this one here just happened to be the first...
additionally sometimes the information is in table[4], other times in table[5]. I did account for this and wrote a function that will determine the correct table. Although I tested the function a LOT and thought it working correctly (hence did not inlcude it above), it did not.
So I made a better xpath:
'/html/body/table[tr/td[#width="20%"]]/tr[position()>1]'
and, although this is not related, I ran into another problem with unxpected encoding in the html file (not utf-8) which was fixed by adding:
parser = etree.XMLParser(encoding='ISO-8859-15')
t = etree.parse(forumfile, parser)
I am now confident that after adjusting for strange additional and multiple , and tags my code will work on all files...
Still I will be looking into lxml.html, as I mentioned in the comment, I have never used it before, but if it is more robust and may allow for using the files without tagsoup, it might be a better fit and save me extensive try/except statements and loops to fix the few files screwing with my current script...
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()