Downloading file names with commas in them using Selenium? - python

So I'm doing a very simple click on link to download file in selenium. It looks something like this:
driver.find_element_by_xpath('element_xpath{0}'.format(i)).click()
Which works just fine. My problem is sometimes chrome throws a ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION.
I googled that to find this question and basically chrome throws that error when there is a comma in the file name and I have verified that this is exactly what is happening in my case as well. Now I realize I might be able to fix this with perhaps the requests library using the same suggestions as the ones in the question above; namely wrapping the file name in quotes or replace the comma with another character.
But my question is, is there any way to handle this issue in selenium? Chrome throws the same error when I manually try to download the file, IE works fine. Switching the selenium driver to IE is something I would like to avoid because it creates a whole lot of other problems.
Any help is appreciated. Thanks.

Related

Upload file using selenium python (through shadow root (open))

I'm trying to upload file to a website using selenium with the chromedriver.
Unfortunately, I can not do that.
Attaches a picture of the components:
inspect
https://i.stack.imgur.com/9SzUv.png
I tried to execute the following code:
input=browser.find_element_by_id('fileInput')
input.send_keys('C:\\picture\\run.gif')
Unfortunately it does not work. I also tried using XPATH. But it fails to identify the object.
Does it have anything to do with it being in shadow-root?
What can be done about it? I tried to read about it, but could not write a code that works. Thank you very much for your help.

Adding markdown highlighted fenced block to django app with markdonx

I have been looking through the documentation but I can’t seem to get this to work. I wish to publish posts from a “creation page”. I want to use the editor markdownx. I got it to work, however there is no “fenced code support”. I tried to add the following line of code to my settings.py
MARKDOWNX_MARKDOWN_EXTENSIONS = ['fenced_code']
but it breaks the code, in fact I get the following error (when I try to create a post):
SyntaxError at /markdownx/markdownify/
if I erase the extensions list, it works fine.
I would like also to use the codehilite extension by adding it to the list like this
MARKDOWNX_MARKDOWN_EXTENSIONS = ['fenced_code' ,'codehilite']
but it does not work. I have installed pigments but the documentation does not seem to help. Could someone help me add this extensions correctly, getting highlighted code blocks to work. Thank you very much.

Internet Shortcut in python

I have a problem. Let's say I have a website (e.g. www.google.com). Is there any way to create a file with a .url extension linking to this website in python? (I am currently looking for a flat, and I am trying to save shortcuts on my hard drive only to apartment offers posted online matching my expectations ) I've tried to use the os and requests module to create such files, but with no success. I would really appreciate the help. (I am using python 3.9.6 on Windows 10)
This is pretty straightforward. I had no idea what .URL files were before seeing this post, so I decided to drag its URL to my desktop. It created a file with the following contents which I viewed in Notepad:
[InternetShortcut]
URL=https://stackoverflow.com/questions/68304057/internet-shortcut-in-python
So, you just need to write out the same thing via Python, except replace the URL with the one you want:
test_url = r'https://www.google.com/'
with open('Google.url','w') as f:
f.write(f"""[InternetShortcut]
URL={test_url}
""")
With regards to your current attempts:
I've tried to use os and requests module to create such file
It's not clear what you're using requests or os for, since you didn't provide a Minimal Reproduceable Example of what you'd tried so far; so, if there's a more complex element to this that you didn't specify, such as automatically generating the file while you're in your browser, or something like that, then you need to update your question to include all of your requirements.

How to run a search engine script in Python?

I am learning how to do webscraping, crawlers etc. and I came across this repo. I understand how the code works, what the input and outputs should be, but how do I run it in a terminal on Windows? How do I call the respective .txt files and test the search engine?
I saw that someone else asked that and the creator showed them this link here. But it still doesn't explain how to actually apply it to files.
The author of logicx24 has hard coded the target text files in querytexts.py. See line 122 which reads:
q = Query(['pg135.txt', 'pg76.txt', 'pg5200.txt'])
The list input to Query are all references to files that exist in the corpus directory. Try changing that to include a different file in their corpus directory. Better yet, add a new target text file of your own and use that.
Good luck!
Why are you using text files? I don't get it. Either way, you could just use Python itself to do that. Use the selenium library for Python. There's a tutorial to installing this here. Once that's done, just use this code if you're using Google:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.google.com")
search = driver.find_element_by_css_selector(".gLFyf.gsfi")
time.sleep(5)
search.send_keys("Desired Input Text Goes Here")
search.send_keys(Keys.RETURN)
Don't worry if it takes a while to load. It usually does that. If you want to reduce the amount of time it takes, use a lower number for the parameter on line 8 (time.sleep(5)). Assuming you've gone ahead and learned a bit more about Selenium, there isn't really much else to talk about apart from one thing. That is, line 7 (search = driver.find_element_by_css_selector(".gLFyf.gsfi"). Assuming you've learned advanced CSS selectors already (if you have literally no experience in web development, specifically HTML and CSS, you can just copy-paste the code), the .gLFyf.gsfi is simply the CSS selector for the search bar in Google. You can find the selector for the search bar in any engine by just looking through the source code using Ctrl + Shift + I on Windows. You can use any other Selenium element selector for this as long as it works. Make sure to also change the URL on line 6 (driver.get("https://www.google.com")) to match that of your search engine if you're not using Google.
Sorry if this seemed a bit vague or strange. If you don't really care, feel free to download Selenium, copy-paste the code, and move on. Otherwise, I suggest also learning Selenium and HTML/CSS if you haven't already.

Differences between rendering HTML in PyCharm and a text editor (Sublime Text)

I've got exactly the same files (HTML + CSS), in both PyCharm and Sublime Text, and the results of rendering these in Google Chrome is completely different.
Editing CSS doesn't have any affect on the results of rendering the HTML.
I have to make the project using Python Flas, but I want to start from HTML and CSS.
Does anybody know why have I different results from the same files?
Let me try a lucky guess since I don't know what is exactly rendering different: it could be the Encoding of the file, you can try and change in Sublime selecting a different enconding type to save the file to match the file saved in pycharm.
File>Save with encoding>[select]
If both are completely equal is the only thing that I can imagine.
When we run PyCharm project it give us the same link and we have to clear cache or cookies every time we open this link

Categories