0

I have the following script in Python but when the page loads the captcha does not work

Can someone help me?

On Windows I have this code that works well for me but on Linux it doesn't work for me

import json
from seleniumbase import SB

with SB(uc=True, test=True, ad_block=True) as sb:
    url = "https://www.nombrerutyfirma.com/"
    sb.activate_cdp_mode(url)
    sb.sleep(2)
    cf_shadow = '[style="display: grid;"] div div'
    if sb.is_element_visible(cf_shadow):
        sb.cdp.gui_click_element(cf_shadow)
    sb.sleep(2)

    sb.open(url)
    sb.wait_for_element('body', timeout=15)
    sb.click('a[href="#rut"]')
    rut_input = sb.wait_for_element_visible('div#rut input[name="term"]', timeout=10)
    rut_input.send_keys("21.405.338-1") 
    rut_input.send_keys("\ue007")
    rows = sb.wait_for_elements('table.table-hover tbody tr', timeout=10)
    data = []
    for row in rows:
        columns = row.find_elements('td')
        record = {
            "Nombre": columns[0].text,
            "RUT": columns[1].text,
            "Sexo": columns[2].text,
            "Dirección": columns[3].text,
            "Ciudad/Comuna": columns[4].text,
        }
        data.append(record)

    json_data = json.dumps(data, ensure_ascii=False, indent=4)
    print(json_data)

How do I take it to seleniumbase and be able to obtain the data. I need help please.

When running the script with "xvfb-run python3 py3.py" it returns this error, This error is because the captcha has not been processed, so the RUT label does not exist

    Haciendo clic en el tab de 'RUT'...
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/dist-packages/seleniumbase/plugins/sb_manager.py", line 1223, in SB
    yield sb
  File "/home/scripts_python/py3.py", line 25, in <module>
    sb.click('a[href="#rut"]')
  File "/usr/local/lib/python3.12/dist-packages/seleniumbase/fixtures/base_case.py", line 395, in click
    self.cdp.click(selector, timeout=timeout)
  File "/usr/local/lib/python3.12/dist-packages/seleniumbase/core/sb_cdp.py", line 633, in click
    element = self.find_element(selector, timeout=timeout)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/dist-packages/seleniumbase/core/sb_cdp.py", line 165, in find_element
    raise Exception(message)
Exception: 
 Element {a[href="#rut"]} was not found after 7 seconds!

I need help please. Thank you.

5
  • Are you able to manually access the website on the linux machine? Commented Jan 17 at 11:25
  • Not just by SSH Commented Jan 17 at 11:27
  • I see...I've never seen this error before, but maybe it has something to do with the virtual display not been recognized by selenium. I have an script that run like this: Xvfb :99 -screen 0 1920x1080x24 & python3 script.py and in my script.py I set the display I'll will use os.environ["DISPLAY"] = ":99". Commented Jan 17 at 11:55
  • Please help me adapt it to what you have so I can execute it. Commented Jan 17 at 11:56
  • Sorry, can't enter now, but I've come up with an answer that may help you. Commented Jan 17 at 12:11

1 Answer 1

0

Without pyvirtualdisplay

Update your script

import json
from seleniumbase import SB
import os

os.environ["DISPLAY"] = ":99"

with SB(uc=True, test=True, ad_block=True) as sb:
    url = "https://www.nombrerutyfirma.com/"
    sb.activate_cdp_mode(url)
    sb.sleep(2)
    cf_shadow = '[style="display: grid;"] div div'
    if sb.is_element_visible(cf_shadow):
        sb.cdp.gui_click_element(cf_shadow)
    sb.sleep(2)

    sb.open(url)
    sb.wait_for_element('body', timeout=15)
    sb.click('a[href="#rut"]')
    rut_input = sb.wait_for_element_visible('div#rut input[name="term"]', timeout=10)
    rut_input.send_keys("21.405.338-1") 
    rut_input.send_keys("\ue007")
    rows = sb.wait_for_elements('table.table-hover tbody tr', timeout=10)
    data = []
    for row in rows:
        columns = row.find_elements('td')
        record = {
            "Nombre": columns[0].text,
            "RUT": columns[1].text,
            "Sexo": columns[2].text,
            "Dirección": columns[3].text,
            "Ciudad/Comuna": columns[4].text,
        }
        data.append(record)

    json_data = json.dumps(data, ensure_ascii=False, indent=4)
    print(json_data)

Then run virtual display using the display 99, which is set on your script.

Xvfb :99 -screen 0 1920x1080x24 & python3 script.py

With pyvirtualdisplay

First you may have to install the pyvirtualdisplay package.

And in case you don't have the xvfb installed (I'm assuming is not the case)

sudo apt install xvfb -y

and then

pip install PyVirtualDisplay

Now update your script

import json
from seleniumbase import SB
from pyvirtualdisplay import Display

# Start the virtual display
display = Display(visible=0, size=(1920, 1080))
display.start()

with SB(uc=True, test=True, ad_block=True) as sb:
    url = "https://www.nombrerutyfirma.com/"
    sb.activate_cdp_mode(url)
    sb.sleep(2)
    cf_shadow = '[style="display: grid;"] div div'
    if sb.is_element_visible(cf_shadow):
        sb.cdp.gui_click_element(cf_shadow)
    sb.sleep(2)

    sb.open(url)
    sb.wait_for_element('body', timeout=15)
    sb.click('a[href="#rut"]')
    rut_input = sb.wait_for_element_visible('div#rut input[name="term"]', timeout=10)
    rut_input.send_keys("21.405.338-1") 
    rut_input.send_keys("\ue007")
    rows = sb.wait_for_elements('table.table-hover tbody tr', timeout=10)
    data = []
    for row in rows:
        columns = row.find_elements('td')
        record = {
            "Nombre": columns[0].text,
            "RUT": columns[1].text,
            "Sexo": columns[2].text,
            "Dirección": columns[3].text,
            "Ciudad/Comuna": columns[4].text,
        }
        data.append(record)

    json_data = json.dumps(data, ensure_ascii=False, indent=4)
    print(json_data)
# close the display
display.stop()
Sign up to request clarification or add additional context in comments.

3 Comments

I tried the code that you sent me, and it gives me the same error that I had before... From what I see, the captcha is not passing, how can we find out if the captcha is passing?
My first idea was for you to try access the website manually, and see if it would pass the captcha, but I guess this is not an option. Are you sure the browser that you're using on the linux machine is updated?
Yes, it is updated because I have other codes with other captcha and they work well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.