1

I want to deploy my Python script on Ubuntu server and call it via cron. On my local Windows machine, I tried headless and it is working perfectly fine, I can even take screenshots in it. But running the script on the server results in errors like the element cannot be found. Can someone tell me what is happening with this?

Error Reproduced:

File "DomainScraper.py", line 30, in <module>
    login = driver.find_element_by_xpath('//a[@href="'+login_url+'"]').click()
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
    'value': value})['value']
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/ubuntu/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[@href="https://account.domaintools.com/log-in/?r=https%3A%2F%2Freversewhois.domaintools.com%2F%3Frefine"]"}
  (Session info: headless chrome=81.0.4044.17)

My code, which is I deployed in the server:

#imports...

options = Options()
options.add_argument('--incognito')
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-extensions')
options.add_argument('--disable-infobars')
options.add_argument('--allow-running-insecure-content')
driver = webdriver.Chrome(options=options)
driver.delete_all_cookies()

driver.implicitly_wait(3)
url = "https://reversewhois.domaintools.com/?refine#q=%5B%5B%5B%22whois%22%2C%222%22%2C%22VerifiedID%40SG-Mandatory%22%5D%5D%5D"
driver.get(url)
driver.save_screenshot("sample.png")
login_url = 'https://account.domaintools.com/log-in/?r=https%3A%2F%2Freversewhois.domaintools.com%2F%3Frefine'
login = driver.find_element_by_xpath('//a[@href="'+login_url+'"]').click()

username = driver.find_element_by_id("username")
password = driver.find_element_by_id("password")
username.send_keys("**********************")
password.send_keys("***************")
# time.sleep(5)
driver.find_element_by_id("password").send_keys(Keys.ENTER)

pageNumber = 0
while True:
    driver.implicitly_wait(3)
    driver.get('https://reversewhois.domaintools.com/?ajax=mReverseWhois&call=ajaxGetPreviewPage&q=%5B%5B%5B%22whois%22%2C%222%22%2C%22VerifiedID%40SG-Mandatory%22%5D%5D%5D&o='+str(pageNumber))
    time.sleep(3)
    pre = driver.find_element_by_tag_name("pre").text
    data = json.loads(pre)
    if data['body']:
        table = data['body']
        tables = pd.read_html(table,skiprows=1)
        df = tables[-1]
        df.to_csv('Domains.csv', mode='a', sep=',',index=False)
        print(df.to_string(index=False))
        pageNumber += 1
        # print(pageNumber)
        continue
    else:
        break

Update:

Tried using and installing both libraries

sudo apt install -y xvfb
pip install pyvirtualdisplay

and added this before launching Chrome

from pyvirtualdisplay import Display

display = Display(visible=0, size=(800, 600))
display.start()

seems not to be working after all. I took a screenshot and got this output:

When I don't use the xvfb library, I only get white screen. enter image description here

I think Selenium cant open the url. What should I do about this?

0buz
  • 3,443
  • 2
  • 8
  • 29

1 Answers1

1

Are you sure the element is visible? I noticed the default windows size is not the same in headless as normal mode.

You could could could try changing window size with:

options.add_argument('window-size=1200x1040')
Nils
  • 89
  • 1
  • 9
  • does window size really matter? I saw in the screenshot the url cannot be accessed. Did you try it in your local machine using headless/not headless mode?. Not including the login part. – draw134 Mar 30 '20 at 14:01
  • Not really, but the default window size is 800*600, so there have been some cases where I found it useful to make it bigger to avoid scrolling to make an object visible to enable click. – Nils Mar 30 '20 at 14:23
  • well even if you make it bigger. the fact that the url cannot be accessed it is not useful. – draw134 Mar 30 '20 at 14:26