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

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.

Related

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 do I scan an image directly on a webpage with an OCR?

What i'm basically trying to do is going on a webpage with selenium and trying to get past a captcha that has been done very poorly. I should mention I'm using python with selenium 4 to scrape said webpage.
In short, this captcha simply displays some numbers that change along with their colors every time I fail to do the captcha and that need to be written in a box in order to pass it, which seems like an extremeley simple thing to get by. I'll attach one of them
here, and an example of a second one after i failed the captcha here.
All i need in a good OCR that manages to read an image directly on the webpage without needing to download it, and store this value to later input it into an input box. Is there any tool that can help me do just that? I looked a little bit online and I couldn't seem to find anything like this. Can anyone suggest me the right library for this and, if you really could, how to read this image?
I would suggest taking a screenshot in Selenium: https://www.guru99.com/take-screenshot-selenium-webdriver.html#:~:text=Taking%20Screenshot%20in%20Selenium%20is,Copy%20file%20to%20Desired%20Location
And then run it through an OCR: https://pypi.org/project/pytesseract/

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.

Downloading file names with commas in them using Selenium?

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.

Executing python file through JSP

I am creating a simple web page which reads from the database and display on the browser.
I have a python script which reads from a file and updates the database for the changes made in the file.
I want to add a button in my webpage which executes this python file and display the updated information on the webpage.
Is there a way to link the execution of the python file to the button present on the webpage.
Any suggestions or reference is highly appreciated.
Thanks in advance.
EDITs:
I went through a couple of reference but those were not of much help.
I am not using jython or any other application that supports both java and python.
Is there a way to do it without using any other application.
The way to do this is the same way you would do it normally in Java.
http://www.java-samples.com/showtutorial.php?tutorialid=8

Categories