click() function in selenium only interactable if VPN is used. [Python] - python

I am writing my script to automate the form filling process for multiple websites.
So I basically targeted all fields using the xpath input type = xxx
But the strange thing is that the half-done script was perfect when running. it didnt show any errors. After a few days, I decided to run the script again to continue my work but the script shows an error now.
textfields = web.find_elements_by_xpath('//input[#type="text"]')
for textfield in textfields:
if textfield.get_attribute('value')!=Name:
textfield.click()
textfield.send_keys(Name)
The variable Name contains a string message of my name. So when the script is run, the error comes from the textfield.click() saying element not interactable. The strange thing is, when I use a VPN from VPNBook, this error does not show up. But the only issue when using the VPN is that I cannot submit the form as it detects me as spam as my IP is from other country.
I would like to know what is causing this error. Am i doing something wrong?
Note: I am using Kali Linux and Python.

Related

Python Selenium - WebDriverException: browsingContextFn() .currentWindowGlobal is null

I wrote an Instagram bot with Python and Selenium, which works just fine. Most of the time.
Sometimes, in save data when it should click "don't save," its randomly throws this error:
WebDriverException: browsingContextFn() .currentWindowGlobal is null
When I restart the program, it just works fine.
My question is: what does this error mean and how can I fix it? I tried to search this but found nothing.
I can post my code if really needed but I'm not that good at coding and it's a little messy.

How to detect error after ineffective submit in selenium

I've just started using Selenium and I can successfully fill a form and click the submit button using the examples code, however I get an error message (which only appears written in the website after submitting, not an actual coding error) which says "There has been an error, try again".
It seems to me the website somehow knows I'm not filling the form manually, and it doesn't like that.
Is that the case ? What can I do in such cases ?
EDIT: This is the website with the form I'm trying to submit:
https://www.alitalia.com/it_it/special-pages/richiesta-rimborso-volo-cancellato.html

Inspecting a code on Safari Web Inspector- and accidentally dragging the code elsewhere: Now Getting ERROR

SAFARI > DEVELOP > SHOW INSPECT: I'm learning python, and saw this cool tutorial- where you use the import web browser command to open google chrome, so I wanted to see if I can automatically login to a webpage as my next task using python. I decided on using Google's Gmail page as the page I would try to log into using python. My goal was to direct python to input my user #gmail address and password so I research the topic and learned that I needed to target the input fields. But, I didn't even get that far, while trying to inspect which "DIV" button that holds the login handle, I accidentally dragged part of the src code to another part of the code in the Show Web Inspect Element, and now Im getting this weird error, and wondering how to reset the code on my safari page so I don't get any errors.
tried cleared cookies using safari- but error still shows. Error is down below. I research developers site, but there aren't any instructions to reset to factory.
Here's the error I get in Safari >> Develop >> Show Inspect - i go to gmail, and when login in w/out a password I get this code: Unrecognized Content-Security-Policy directive 'worker-src'.
After password is inputed, I get 21 error codes- mainly this:
[Error] The source list for Content Security Policy directive 'script-src' contains an invalid source: ''strict-dynamic''. It will be ignored.
How can I reset safari, or how do I resolve this error on Safari?
Question is different from the other- issue.

Selenium in Python doesn't recognise changes in the DOM even after waiting

I'm using selenium on a Python script to get some data and execute some scripts on a javascript/ajax rendered website, which means some buttons or scripts will change the DOM without loading a new page or changing the URL.
The situation is: as soon as the page is loaded, I execute the following script:
driver1.execute_script("javascript:changeOption('a', 'b','otherPage.html');")
changeOption is a script written directly inside the script tags of the HTML. It works, and the script will cause the DOM to change.
Now, the DOM has changed and there are new scripts written directly on the HTML, which I can clearly see inside Chrome's inspector "Elements" tab. I wait some seconds and try to run one of the new scripts:
driver1.execute_script("javascript:filterOption(0);")
And I get an error! It says this script 'filterOption' doesn't exist.
After some investigation, I notice selenium isn't recognising that the DOM has changed and that there are new elements and scripts inside the page! That's why it says the new script doesn't exist.
What I've tried so far:
Implicit and explicit waits. Neither worked.
time.sleep(), with several seconds, before executing the second script. Didn't work either.
Forcibly try to execute the script with "try...except" statement inside a while loop. Never works, always goes to the exception.
Checked whether the page selenium is 'seeing' is the updated page, by executing again the first script, checking for some elements, waiting for new elements to appear etc. No, it isn't: selenium really hasn't noticed the DOM was updated, that's for sure.
I did some research here and on the documentation looking for some way to force selenium to read the DOM again without refreshing the page, because refreshing would cause it to go back to the very first page and lose all the changes. It looks like there is no way to do it and every single answer I found was different and didn't work.
Do you have any suggestions of what I could do so Selenium will notice there are new scripts on the DOM?
Thank you!
P.S: I'm using the latest selenium with Python 3 and Chrome.
SOLVED!
It turns out the new code is rendered inside an iFrame, that's why selenium couldn't see it before.

Handle random ForeSee popup using Python and Selenium

I'm new to coding and trying to use Selenium with Python to click through a website and fill a shopping cart. I've got things working well except for the random ForeSee survey popup. When it appears (and it doesn't always appear in the same location), my code stops working at that point.
I read the ForeSee documentation and it says "...when the invitation is displayed, the fsr.r...cookie is dropped. This cookie prevents a user from being invited again for X days (default 90)."
Hoping for a quick fix, I created a separate Firefox profile and ran through the website and got the ForeSee pop up invitation--no more pop up when manually using that profile. But I still get the pop up when using Selenium.
I used this code:
fp = webdriver.FirefoxProfile('C:\path\to\profile')
browser = webdriver.Firefox(firefox_profile=fp)
EDIT: I got the cookie working. I was using the Local folder instead of the Roaming folder in C:\path\to\profile. Using the roaming folder solved the problem.
My question edited to delete the part about the cookie not working:
Can someone suggest code to permanently handle the ForeSee pop up that appears randomly and on random pages?
I'm using using Protractor with JS, so I can't give you actual code to handle the issue, but I can give you an idea how to approach this.
In a nutshell
When following script is executed in the browser's console -
window.FSR.setFSRVisibility(true);
it makes ForeSee popup appear behind the rest of HTML elements. And doesn't affect UI tests anymore
So my protractor script will look like so
await browser.executeScript(
`window.FSR.setFSRVisibility(true);`
);
Theory
So ForeSee is one of those services that can be integrated with any web app, and will be pulling js code from their API and changing HTML of your app, by executing the code on the scope of the website. Another example of such company is walkme
Obviously in modern world, if these guys can overlay a webpage, they should have a configuration to make it optional (at least for lower environments) and they actually do. What I mentioned as a solution came from this page. But assuming they didn't have such option, one could reach out their support and ask how to workaround their popups. Even if they didn't have such option they would gladly consider it as a feature for improvement.

Categories