0

I am working on a site that has a form and the user is supposed to upload a photo on the site. The form checks the heigh weight and size of the image to make sure it is large. I have a bot that is running on the site and is getting stuck because some of the images it puts are too small. For my purposes, I want to be able to override the JavaScript on the source of the page from my selenium script written in Python, so that when I submit a small file it just goes through anyway.

Here is a screenshot of the JavaScript source code:

From website

I am not able to directly edit this but I was able to set a break point before the if statement in the developer tools and then write "h=300;","w=300;", and "size=300;: to get it to work, however I basically need to be able to do this from within selenium using the webdriver.

I was able to get what I want working from within Chrome itself with a breakpoint and manually entering the variables to be different but I couldn't figure out how to do it from within my bot. This how I set up my driver:

This how I set up my driver

I have looked all over the internet and couldn't find anything on the topic. It seems like this would be something that would work well with a content script if I had a Chrome extension, however this project took me hours and it would be a shame if I had to rewrite everything in JavaScript and then make an extension just for this. If there is a simple way to do that though I would be all ears.

2
  • 1
    Please provide enough code so others can better understand or reproduce the problem. Commented Apr 2, 2022 at 3:59
  • always put code, data and full error message as text (not screenshot, not link) in question (not in comment). It will be more readable and easier to use in answer, and more people will see it - so more people can help you. Commented Apr 3, 2022 at 13:22

2 Answers 2

1

Unless that object is in global scope there's no way to do it with javascript but you might be able to intercept and rewrite the response with selenium or with a proxy.

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, it isn't in the global scope. I can look into intercepting the response I guess. Didn't even know selenium would let you do that.
Look into browser mob
0

(almost duplicated to https://stackoverflow.com/a/77251290/9758790)

I suggest using the extension Resource Override, as stated in answer. Just replace the target script with your file. I think this extension is a good alter to Chrome's DevTools Local Overrides since it can do the overrides automatically.

I installed and configure the extension in Chrome, and then use

options = Options()
options.add_argument("--user-data-dir=C:/Users/USERNAME/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(options=options)

to load the user data to restore the setting of the extension. I am not sure whether there is a more elegant way to load the setting of the extension.

FYI: If you want to change the setting of the extensions programmatically, you can refer to this answer. So perhaps you can install the crx as suggested by answer and configure the extension in this way. However, I think the previous solution is more convenient.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.