2

我最近在学习 python,但我有一些错误。

环境 python3 , chrome , webdriver(chrome)

from selenium import webdriver
import time
import random
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

driver = webdriver.Chrome("./chromedriver.exe") 

mobile_emulation = { "deviceName": 'Nexus 5' }
chrome_options = webdriver.ChromeOptions()


chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)


driver = webdriver.Remote(command_executor='https:xxx.com',desired_capabilities = chrome_options.to_capabilities())


driver.get("https:/xxx.com")

num = random.randint(11111111 , 99999999)

red = driver.find_element_by_class_name("***")
red.click()

numBox = driver.find_element_by_name("***")
numBox.send_keys(int(num))

reader = driver.find_element_by_id("***")
reader.send_keys("***")

comment = driver.find_element_by_css_selector(" ***")
comment.click()

结果错误在这里

Traceback (most recent call last):
  File "C:\python\pad\pad.py", line 16, in <module>
    driver = webdriver.Remote(command_executor='https:xxx.com',desired_capabilities = chrome_options.to_capabilities())
  File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 156, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 254, in start_session
    self.session_id = response['sessionId']
TypeError: string indices must be integers

我认为错误是因为此代码的数量包括十进制。但我找不到这样的号码。

请给我建议

4

1 回答 1

1

此错误消息...

Traceback (most recent call last):
  File "C:\python\pad\pad.py", line 16, in <module>
    driver = webdriver.Remote(command_executor='https:xxx.com',desired_capabilities = chrome_options.to_capabilities())
.
TypeError: string indices must be integers

...暗示调用方法时出现TypeError 。webdriver.Remote()

根据您webdriver.Remote()与参数一起使用的代码试验,您command_executor可能正试图在Selenium Grid Configuration中执行您的测试。

根据文档文档

  • command_executor:remote_connection.RemoteConnection 对象用于执行命令。

    • 例子:

      command_executor='http://127.0.0.1:4444/wd/hub'
      
    • 完整的实现:

      driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities = chrome_options.to_capabilities())
      

注意:这里我们已经考虑到Selenium Grid HubSelenium Grid Node已经配置好,并在本地主机上使用默认配置成功运行。

解决方案(Python 3.6)

您的有效代码块将是:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions() 
chrome_options.add_argument("start-maximized")
chrome_options.add_argument('disable-infobars')
#driver = webdriver.Remote(command_executor='https:xxx.com', desired_capabilities = chrome_options.to_capabilities())
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities = chrome_options.to_capabilities())
driver.get('https://www.google.co.in')
print("Page Title is : %s" %driver.title)
driver.quit()
于 2018-09-17T08:34:19.630 回答