我迭代a google form survey并尝试放入一些内容(以防万一,我试图使其看起来像是引号)。但是某些字段是年龄的,不允许这样超过99岁:
<input type="text" class="quantumWizTextinputPaperinputInput exportInput" jsname="YPqjbf" autocomplete="off" tabindex="0" aria-label="Age" aria-describedby="i.desc.504994172 i.err.504994172" name="entry.128750970" value="" min="18" max="99" required="" dir="auto" data-initial-dir="auto" data-initial-value="10102015" badinput="false" aria-invalid="true">
所以我在代码中添加了一个条件,以尝试查看我必须在上面写的元素上是否有'max'属性:
content_areas = driver.find_elements_by_class_name(
"quantumWizTextinputSimpleinputInput.exportInput"
)
for content_area in content_areas:
if content_area.get_attribute("max") exists:
max = content_area.get_attribute("max")
content_area.send_keys(max)
else:
content_area.send_keys("10102015")
但是它不起作用:
max:
Traceback (most recent call last):
File "questions_scraper_michael.py", line 151, in <module>
result = extract(driver, df, column)
File "questions_scraper_michael.py", line 70, in extract
"freebirdFormviewerViewNumberedItemContainer"
File "C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 580, in find_elements_
by_class_name
return self.find_elements(by=By.CLASS_NAME, value=name)
File "C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 1007, in find_elements
'value': value})['value'] or []
File "C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\antoi\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 241, in check_respo
nse
raise exception_class(message, screen, stacktrace, alert_text)
selenium.common.exceptions.UnexpectedAlertPresentException: Alert Text: {Alert text :
Message: unexpected alert open: {Alert text : }
(Session info: chrome=83.0.4103.61)
我认为您的解决方案应该是这样
content_areas = driver.find_elements_by_class_name(
"quantumWizTextinputSimpleinputInput.exportInput"
)
for content_area in content_areas:
if content_area.get_attribute("max") and not content_area..get_attribute("max").isspace():
max = content_area.get_attribute("max")
else:
content_area.send_keys("10102015")
[python中没有“ exists”命令。您应该将其删除。
for content_area in content_areas:
if content_area.get_attribute("max"):
max = content_area.get_attribute("max")
else:
content_area.send_keys("10102015")