selenium “NoneType”对象没有属性“find_element”,尝试使用条件切换选项卡

s4chpxco  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(163)

我尝试在满足条件时切换选项卡,然后将值放入新选项卡中。
在这种情况下,条件是如果一个数字以“S”开头,如S9335,则将其替换为900,即S9335=9009335,并使用下一个代码在另一个网站中搜索该值,并提取该网站中出现的值,如果该值不以“S”开始,则在另一个网站中搜索该值:

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)

但我有下一个错误

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

有人帮忙吗?,拜托

编辑

如果我将d1替换为driver,您可以在下面的代码中看到

driver.get(websites[1])

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

下一个错误

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]``
n9vozmp4

n9vozmp41#

将d1改为driver:

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

你调用了undefined变量,这就是运行时错误的原因。

相关问题