You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

启动WebDriver远程会话遇TypeError:字符串索引必须为整数

Fixing "TypeError: string indices must be integer" in Your Selenium Firefox Test

Hey there, let's break down why you're hitting this error and get your test up and running smoothly.

What's Causing the Error?

The root issue lies in your command_executor setting when initializing webdriver.Remote. You've pointed it to http://127.0.0.1:80—your IIS server's address—but this parameter is meant for connecting to a Selenium Grid Hub, not your web application.

When Selenium tries to communicate with IIS, it gets a web page response (a plain string) instead of the JSON-formatted command responses it expects from a Grid hub. Selenium then attempts to parse that string like a dictionary, which triggers the TypeError: string indices must be integer because you can't use dictionary-style indexing on a string.

How to Fix It

You have two clear options depending on your testing setup:

Option 1: Run Firefox Locally (No Remote Needed)

If you're only testing on your local machine, you don't need webdriver.Remote at all. Use the regular Firefox driver with your custom profile instead:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

class CheckGeoLocationsDropDown(): 
    def test_check_geo_locations_dropdown(self): 
        try: 
            selenium_profile = webdriver.FirefoxProfile() 
            # Auto-approve geolocation requests
            selenium_profile.set_preference('media.navigator.permission.disabled', True)
            # Configure SOCKS proxy
            selenium_profile.set_preference("network.proxy.socks", '127.0.0.1') 
            selenium_profile.set_preference("network.proxy.socks_port", 80) 
            selenium_profile.update_preferences() 
            
            baseUrl = 'http://127.0.0.1:80' 

            # Initialize local Firefox driver with your custom profile
            driver = webdriver.Firefox(firefox_profile=selenium_profile) 
            
            driver.maximize_window() 
            driver.get(baseUrl) 
            driver.implicitly_wait(5) 
            driver.refresh() 
            
            # Add your dropdown selection logic here
            # _select_geo_locations_dropdown(driver) 
            
            print('*' * 40 + 'Geo Locations Dropdown selected successfully' + '*' * 40) 
            driver.close() 
        except NoSuchElementException: 
            print("Couldn't Run Script - Target element not found")
        except Exception as e:
            print(f"Unexpected error occurred: {str(e)}")
            if 'driver' in locals():
                driver.quit()

ff = CheckGeoLocationsDropDown() 
ff.test_check_geo_locations_dropdown()

Option 2: Use Remote WebDriver with Selenium Grid

If you need to use a remote Selenium Grid (e.g., for cross-browser testing), first ensure your Grid Hub is running. Then update the command_executor to point to the Grid's default address (usually http://127.0.0.1:4444/wd/hub):

# Update the Remote driver initialization section
driver = webdriver.Remote(
    desired_capabilities=capabilities, 
    command_executor='http://127.0.0.1:4444/wd/hub',  # Correct Grid Hub URL
    browser_profile=selenium_profile
)

Extra Tips to Avoid Future Issues

  • Confirm your Firefox version is compatible with your installed Selenium version—version mismatches often cause unexpected bugs.
  • Verify that your SOCKS proxy at 127.0.0.1:80 is actually running; if not, Firefox will fail to load your web app.
  • For granular geolocation control (instead of disabling all media permissions), use selenium_profile.set_preference('permissions.default.geo', 1) (1 = allow, 2 = block).

内容的提问来源于stack exchange,提问作者Sarvesh Galgalikar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:43:38