Python By定位元素,遇到问题:
定位一个输入框,无id、name、_TEXT等,尝试通过By.CLASS_NAME、By.TAG_NAME定位元素然后输入,定位时HTML中有多个,尝试使用
input_elements = driver.find_elements(By.CLASS_NAME,"nui-ipt-input")
if input_elements:
first_input = input_elements[2]
first_input.send_keys("测试")
upload_images = driver.find_element(By.TAG_NAME,"input")
if upload_images:
images_input = upload_images[10]
images_input.click().send_keys("C:\上传文件\测试.jpg")
可以定位并输入,但是后续步骤中再次定位一个输入框,然后输入时,使用By.CLASS_NAME或By.TAG_NAME,都失败,如下:
upload_images = driver.find_element(By.TAG_NAME,"input")
if upload_images:
images_input = upload_images[10]
images_input.click().send_keys("C:\上传文件\测试.jpg")
或
upload_images1 = driver.find_element(By.CLASS_NAME,"nui-ipt-input")
if len(upload_images1) >= 5:
upload_images1[4].send_keys("C:\上传文件\测试.jpg")
else:
print('没有足够的元素供选择')
都失败了,提示:
images_input = upload_images[10]
~~~~~~~~~~~~~^^^^
TypeError: 'WebElement' object is not subscriptable
if len(upload_images1) >= 5:
^^^^^^^^^^^^^^^^^^^
TypeError: object of type 'WebElement' has no len()
各位帮忙看下。