Selenium code working fine on main laptop, but not on my other one

46 Views Asked by At

I wrote some code in Pycharm last year, to loop through VAT numbers entered into the Gov website, to make sure they were still valid. It still works fine on the original laptop, but not on my other laptop, even thought the code is exactly the same (the only adjustment was for the location of the spreadsheet). I have given my code down below. When I try to run it on my other laptop, it comes up with the following error.

Traceback (most recent call last):
  File "C:\Users\neils\Documents\Pycharm Projects\VATChecker\VATChecker.py", line 30, in <module>
    VAT = web.find_element_by_xpath('//*[@id="target"]')
          ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'WebDriver' object has no attribute 'find_element_by_xpath'

Process finished with exit code 1

One thing I also notice, is that the import sys and import datetime are both greyed out. I guess that's because it crashed before these imports were used?

I should mention, the version of Chrome is the same for both laptops (version 123), and I have the same chromedriver installed for both. Both laptops are windows 64 bit, in case you're thinking that might be the issue.

Are you able to advise me what the problem is please?

import datetime
import sys

from selenium import webdriver
from openpyxl import workbook, load_workbook
from datetime import date

web = webdriver.Chrome()

wb = load_workbook('C:\\Users\\neils\\Documents\\NSO\\Self Billing agreements\\VATMusiciansCheckerUK.xlsx', data_only=True, read_only=False)

ws = wb.active
x=2
y=1
current_datetime = datetime.datetime.now()
current_datetime.strftime('%x %X')

invalid = ""

while ws.cell(x,1).value !=None:

    ws.cell(x,9).value = ""

    web.get("https://www.tax.service.gov.uk/check-vat-number/enter-vat-details")

    web.implicitly_wait(10)

    VatNumber = ws.cell(x,4).value

    VAT = web.find_element_by_xpath('//*[@id="target"]')
    VAT.send_keys(VatNumber)

    VAT.submit()

    web.implicitly_wait(4)

    registered = web.find_element_by_xpath('/html/body/div[2]')
    if (registered.text.find("Invalid")) > 0:
        ws.cell(x,9).value = "Invalid VAT number"
        invalid = invalid + str(y) + " " + ws.cell(x,1).value + " " + ws.cell(x,2).value + ", "
        y=y+1
    else:
        ws.cell(x,9).value = "Valid VAT number"

        ws.cell(x,6).value = current_datetime

    x=x+1

if invalid == "":

    print("All VAT records are correct")
else:
   print("Invalid VAT records are " + invalid)


wb.save('C:\\Users\\neils\\Documents\\NSO\\Self Billing agreements\\VATMusiciansCheckerUK.xlsx')


2

There are 2 best solutions below

0
Ammar On

Use

web.find_element(By.XPATH, "xpath")

Instead of

web.find_element_by_xpath("xpath")

Don't forget to add

from selenium.webdriver.common.by import By

For more reference refer to this.

1
Stephen C On

Your code is using deprecated methods that were removed in Selenium version 4.3.0; see https://github.com/SeleniumHQ/selenium/blob/trunk/py/CHANGES.

You could pip install an older version of Selenium as a workaround. But a better idea would be to change your code to replace the calls to the deprecated calls with web.find_element(...) calls, as described in the current Selenium documentation.

It would also be a good idea to create a "requirements.txt" file for your code, and constrain the version for the Selenium python library.


I expect that the reason that your two laptops behave differently is that you have different versions of the Selenium python library installed. (If you don't constrain the required version of a library, pip will install the latest version by default.)