Forums

Different XPATH in selenium

Hi,

I'm experimenting with some marketing automation techniques on Instagram in Selenium. When I run the script locally (on my Mac, in the latest Chromium) everything works. However, when I push the code to a Phyton anywhere server, I have issues with finding the correct elements. It looks like the XPATHs are different.

I added all the arguments to the Chrome options (as suggested by the Phyton anywhere Selenium tutorial) and also figured out that the Chrome driver on the server is an older version than the one on my own device. When searching for the elements in this older version on my Mac, again they do not seem to correspond with the ones on the server.

Can anyone help me find the correct XPATHs so I can run it on your (great!) service? Apologies if this question has already been raised on the forum but I could not find it...

Thanks!

Could you give some specific example?

Hi fjl,

Thank you very much for your quick reply!

Sure! The login procedure works flawless, this probably because I use the

find_element(By.XPATH, '//input[@name="username"]') type XPATHS.

Bur, for example, when I want to open the search button with:

find_element(By.XPATH, "/html/body/div[2]/div/div/div/div[1]/div/div/div/div[1]/div[1]/div[1]/div/div/div[1]/div/div[2]/div[2]/div/a")

I get the error "element not found".

Looking forward to your reply!

Kind regards, Brecht

You can add a breakpoint in the script and try to debug it step by step -- it's hard to guess where the issue may be. It could be possible that you may need to add some wait for element presence before you run find_element.

Hi Pafk,

I'm aware that I can add breakpoints. I'm sure that the error arises because I'm using the wrong XPATH. I'm using a:

while True: try: find_element break except: sleep(1)

structure to wait until the element is present. Is there any way I could debug with the exact chrome driver Pythonanywhere is using?

Kind regards!

I see you're using older system image (glastonbury) -- if you use the default chromedriver, it's:

$ chromedriver --version                                                                                
ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429})

Hi Pafk,

I updated my system image to "haggis" as there was no specific reason for me to be working on an older version. Then I downloaded on my local machine the specific ChromeDriver you mentioned here above. And indeed as expected the XPATHS in this older version are different from the ones I obtained from the most recent version. But after running the script with the new XPATHS on pythonanywhere I still have the problem that the XPATHS doesn't seem to relate to an element. Could this be because I'm working on a mac and the server is Linux based? If so, is there any possibility you can think of to obtain the correct XPATHS?

Kind regards! Brecht

You could try debugging it in the headless mode on PA using screenshots -- maybe state of the page is different in the headless mode?