TypeError: 'WebElement' object is not subscriptable error submitting form using Selenium and Python
TypeError: 'WebElement' object is not subscriptable error submitting form using Selenium and Python
我正在尝试自动提交他在 cod 上的精彩表演的 twitch 剪辑,但遇到了一些问题。
这是我设置的测试表格,与正常提交的剪辑相同 - https://forms.gle/MDMM3buW2DT5erpp8
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument("-incognito")
browser = webdriver.Chrome(executable_path='/Users/Goldilocks/Downloads/chromedriver', options=option)
browser.get("https://forms.gle/MDMM3buW2DT5erpp8")
clipDescription = "streamer sticks juggernaut with semtex for the win!"
clipLink = "https://twitch.tv/joocylad"
textboxes = browser.find_elements_by_class_name("quantumWizTextinputPaperinputInput")
radiobuttons = browser.find_elements_by_class_name("docssharedWizToggleLabeledLabelWrapper")
submitbutton = browser.find_element_by_class_name("appsMaterialWizButtonPaperbuttonContent")
radiobuttons[3].click()
radiobuttons[2].click()
textboxes[0].send_keys("JoocyLad")
textboxes[1].send_keys(clipLink)
textboxes[2].send_keys(clipDescription)
textboxes[3].send_keys("email")
submitbutton[0].click()
browser.close()
代码还没有完全写完,我打算把clipLink
和clipDescription
变成程序在运行时接受输入的变量,但我还没有得到到那个为止。
我遇到的问题是没有填写第二个选择题。我也得到错误:
Traceback (most recent call last):
File "/Users/Goldilocks/PycharmProjects/pythonProject/test.py", line 31, in <module>
submitbutton[0].click()
TypeError: 'WebElement' object is not subscriptable
我在 google chrome 版本 87.0.4280.88 和 chrome 驱动程序是同一版本,87.0.4280.88
我认为问题在于 find_element_by_class_name()
returns 单个项目,而不是列表。删除 [0],它应该可以工作。也看看here
这个错误信息...
TypeError: 'WebElement' object is not subscriptable
...表示您已将索引添加到 中,该索引不可订阅。索引可用于访问 list.
的元素
find_element_by_class_name()
find_element_by_class_name()
在此元素的子元素中按 class 名称查找元素。
作为 find_element_by_class_name()
returns 单个元素,它没有索引且不可订阅。
解决方案
您需要从行 submitbutton[0].click()
中删除索引。所以你的有效代码行将是:
submitbutton.click()
我正在尝试自动提交他在 cod 上的精彩表演的 twitch 剪辑,但遇到了一些问题。
这是我设置的测试表格,与正常提交的剪辑相同 - https://forms.gle/MDMM3buW2DT5erpp8
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_argument("-incognito")
browser = webdriver.Chrome(executable_path='/Users/Goldilocks/Downloads/chromedriver', options=option)
browser.get("https://forms.gle/MDMM3buW2DT5erpp8")
clipDescription = "streamer sticks juggernaut with semtex for the win!"
clipLink = "https://twitch.tv/joocylad"
textboxes = browser.find_elements_by_class_name("quantumWizTextinputPaperinputInput")
radiobuttons = browser.find_elements_by_class_name("docssharedWizToggleLabeledLabelWrapper")
submitbutton = browser.find_element_by_class_name("appsMaterialWizButtonPaperbuttonContent")
radiobuttons[3].click()
radiobuttons[2].click()
textboxes[0].send_keys("JoocyLad")
textboxes[1].send_keys(clipLink)
textboxes[2].send_keys(clipDescription)
textboxes[3].send_keys("email")
submitbutton[0].click()
browser.close()
代码还没有完全写完,我打算把clipLink
和clipDescription
变成程序在运行时接受输入的变量,但我还没有得到到那个为止。
我遇到的问题是没有填写第二个选择题。我也得到错误:
Traceback (most recent call last):
File "/Users/Goldilocks/PycharmProjects/pythonProject/test.py", line 31, in <module>
submitbutton[0].click()
TypeError: 'WebElement' object is not subscriptable
我在 google chrome 版本 87.0.4280.88 和 chrome 驱动程序是同一版本,87.0.4280.88
我认为问题在于 find_element_by_class_name()
returns 单个项目,而不是列表。删除 [0],它应该可以工作。也看看here
这个错误信息...
TypeError: 'WebElement' object is not subscriptable
...表示您已将索引添加到
find_element_by_class_name()
find_element_by_class_name()
在此元素的子元素中按 class 名称查找元素。
作为 find_element_by_class_name()
returns 单个元素,它没有索引且不可订阅。
解决方案
您需要从行 submitbutton[0].click()
中删除索引。所以你的有效代码行将是:
submitbutton.click()