Forums

Cant find elements in selenium

Hey, I just hosted an app that uses selenium to automate Facebook. The script works fine on my local computer but it doesn't work on pythonanywhere. The bot only successfully logs in to Facebook and then can find the respective elements in the further automation process. Why does it happen?

At first, I thought, it happens because the webpage on the chrome headless window opens slowly on hosted app compared to local PC. I increased the time.sleep() seconds between two elements but still it didn't work.

Probably the best way to debug that would be to use Selenium's ability to take screenshots of what is showing on the browser. If you have a webdriver called browser then you can do this:

browser.get_screenshot_as_file(filename)

...then you can see what's on the screen and hopefully that will make it possible to figure out what's going wrong.

I debugged a brief. Let me brief my process. When the script starts, the bot logins into the Facebook page. Then I redirect the browser directly to one of the post by webdriver.get(url). But when entering this URL, the page again asks to log in. Why does it happen when I have logged in already. Does this code webdriver.get(url) open another browser? Because the same script works fine in the local server where the bot opens the URL in the same browser thus not needing to log in again.

.get should not open a different browser. Perhaps your login has failed for some reason. Check the page that you get back when you tried to log in to see if your login was successful.

Hey, even if I have correct login credentials, I am seeing incorrect username or password everytime I try in the screenshots. Why is this happening?

Also when I looked at the screenshots, the Facebook website was having the Spanish language. How to handle language in the webdriver?

Something like https://sqa.stackexchange.com/questions/9904/how-to-set-browser-locale-with-chromedriver-python ?

Yeah, got it. Thanks :-)

Hi Eya, Did you already find a solution to your problem? Unfortunately, I face the same problem. If I run the script on pythonanywhere, my bot cannot find the elements. However, if I run the script on my local machine it works fine. I also increased the time.sleep(), but that does not work for me either. I also took a screenshot to debug. It turns out that the page looks very different when I use webdriver.get(url) if the script is executed via pythonanywhere. What causes this? Any suggestions?

In what way does the site look different in the screenshots when you access it from PythonAnywhere? If it's a "forbidden" error message about the site not being on the list of whitelisted pages for free accounts then that means you'd need to upgrade to a paid account to access it. However, it the site is whitelisted and is just sending back different content in the two environments, it may well be that the site you're trying to access is showing different pages to viewers based on their location, so that wouldn't be something you could fix. (For example, some sites return different content to browsers running in cloud computing environments as a security measure -- and others return different content based on the geographical location of the person viewing it.)