0

I'm trying to switch tab when a condition is satisfied and then in put the value in the new tab.

In this case the condition is if a number start with "S" like S9335 then replace it with a 900,i.e S9335=9009335, and search that value in another website with the next code and extract the value that appear in that website, if that value doesn't begin with "S" then search that value in another website:

options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(service=webdriver_service, options=options)
websites = ['https://www.rues.org.co/RM','https://www.rues.org.co/ESAL']

Mat_column= 'Mat'
value='01'
excel_file_path=r'C:\Users\Hi\Documents\Project\output\164115.xlsx'

driver.get(websites[0])


df=pd.read_excel(excel_file_path, dtype=str)

NIT=[]

for i in df.index: 

    entry=df.loc[i]
   
    if entry[Mat_column].startswith('S'):

        new_entry=entry[Mat_column].replace('S','900')
        
        driver.execute_script("window.open('');")
        driver.switch_to.window(driver.window_handles[1])
        d1=driver.get(websites[1])

        cam = Select(d1.find_element(By.XPATH, '//*[@id="codCamara"]'))
        cam.select_by_value(value)

        driver.find_element(By.LINK_TEXT, 'Matrícula / Inscripción').click()

        time.sleep(3)
         
        mat = driver.find_element(By.XPATH,'//*[@id="txtSearchMat"]')
        consult = driver.find_element(By.XPATH,'//*[@id="btnConsultaMat"]')

        mat.send_keys(new_entry)
        consult.click()
        mat.send_keys(Keys.CONTROL + "a")

        try:
            nit = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="rmTable2"]/tbody/tr/td[3]'))).text
            NIT.append(nit)
        except:
            NIT.append("0")

    else:
        driver.execute_script("window.open('');")
        driver.switch_to.window(driver.window_handles[1])

        cam = Select(d1.find_element(By.XPATH, '//*[@id="codCamara"]'))
        cam.select_by_value(value)

        driver.find_element(By.LINK_TEXT, 'Matrícula / Inscripción').click()

        mat = driver.find_element(By.XPATH,'//*[@id="txtSearchMat"]')
        consult = driver.find_element(By.XPATH,'//*[@id="btnConsultaMat"]') 

        mat.send_keys(entry[Mat_column])
        consult.click()
        mat.send_keys(Keys.CONTROL + "a")

        try:
            nit = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="rmTable2"]/tbody/tr/td[3]'))).text
            NIT.append(nit)
        except:
            NIT.append("0")

DATANIT=pd.DataFrame(
    {   
        'NIT' : NIT,
    }
)

print(DATANIT)

but I have the next error

cam = Select(d1.find_element(By.XPATH, '//*[@id="codCamara"]'))
                    ^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'find_element'

Any help? please

EDIT

If I replace the d1 with driver as you can see in the next code below

driver.get(websites[1])

cam = Select(driver.find_element(By.XPATH, '//*[@id="codCamara"]'))

I have the next error

DevTools listening on ws://127.0.0.1:52945/devtools/browser/4f3a5ee5-3ad5-4ab0-9477-65e3a8d2302f
Traceback (most recent call last):
  File "c:\Users\Hi\Documents\Alaska\alaska_validator\validacion_nit_vacios.py", line 67, in <module>
    cam.select_by_value(value)
  File "C:\Users\Hi\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\select.py", line 76, in select_by_value
    self._set_selected(opt)
  File "C:\Users\Hi\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\select.py", line 211, in _set_selected
    option.click()
  File "C:\Users\Hi\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 94, in click  
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Hi\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 403, in _execute
    return self._parent.execute(command, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Hi\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute    self.error_handler.check_response(response)
  File "C:\Users\Hi\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: Element is not currently visible and may not be manipulated
  (Session info: chrome=113.0.5672.93)
Stacktrace:
Backtrace:
        GetHandleVerifier [0x00808893+48451]
        (No symbol) [0x0079B8A1]
        (No symbol) [0x006A5058]
        (No symbol) [0x006A83F1]
        (No symbol) [0x006A9631]
        (No symbol) [0x006A96D0]
        (No symbol) [0x006D2DE0]
        (No symbol) [0x006C9E74]
        (No symbol) [0x006EA2BC]
        (No symbol) [0x006C9586]
        (No symbol) [0x006EA614]
        (No symbol) [0x006FC482]
        (No symbol) [0x006EA0B6]
        (No symbol) [0x006C7E08]
        (No symbol) [0x006C8F2D]
        GetHandleVerifier [0x00A68E3A+2540266]
        GetHandleVerifier [0x00AA8959+2801161]
        GetHandleVerifier [0x00AA295C+2776588]
        GetHandleVerifier [0x00892280+612144]
        (No symbol) [0x007A4F6C]
        (No symbol) [0x007A11D8]
        (No symbol) [0x007A12BB]
        (No symbol) [0x00794857]
        BaseThreadInitThunk [0x761500C9+25]
        RtlGetAppContainerNamedObjectPath [0x775A7B4E+286]
        RtlGetAppContainerNamedObjectPath [0x775A7B1E+238]``

Al.Vioky
  • 23
  • 7
  • 2
    Based on the [quickstart instructions](https://selenium-python.readthedocs.io/getting-started.html), it looks like `d1.find_element` ought to be `driver.find_element`. – Nick ODell May 16 '23 at 21:17
  • The Selenium `driver.get` does not return anything. You only have one connection to the browser. You can't get one connection per tab. – Tim Roberts May 16 '23 at 21:20
  • @NickODell but if I do that i have the next error line 76, in select_by_value self._set_selected(opt)File C:\Users\Hi\AppData\Local\Programs\Python\Python311\Lib\site packages\selenium\webdriver\support\select.py", line 211, in _set_selected option.click() – Al.Vioky May 16 '23 at 21:22
  • @Al.Vioky You may have more than one error. Can you include the full traceback in your question? – Nick ODell May 16 '23 at 21:24
  • @NickODell already did, caun u see the edit? thank u! – Al.Vioky May 16 '23 at 21:28
  • https://stackoverflow.com/questions/43868009/how-to-resolve-elementnotinteractableexception-element-is-not-visible-in-seleni – Nick ODell May 16 '23 at 21:31

1 Answers1

0

It is a known issue with Selenium that you inspect an element on the browser. Yet, when you try to click it Selenium tells you that this element is not clickable or not interactable.

For this you have 2 options:

  1. Click on the 1st child of the object:

    cam = 
    Select(d1.find_element(By.XPATH, '//* 
    [@id="codCamara"]/span'))
    

    Or, if the inner child is div:

    cam = Select(d1.find_element(By.XPATH,
    '//*[@id="codCamara"]/div'))
    
  2. Try to click on the first parent ("dad") element:

     cam = Select(d1.find_element(By.XPATH,
    '//*[@id="codCamara"]'))
     parent = cam.findElement(By.xpath("./.."))
    
Tal Angel
  • 1,301
  • 3
  • 29
  • 63