今天定位一个页面中的input文本框,发现竟然有两个几乎一模一样的html代码的input文本框。


唯一不同的是,一图中的input有一个comps="[object Object],[object Object]"的属性和属性值,二图则没有。我要定位的是二图中的input,并且输入内容。
代码如下:
- from selenium import webdriver
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.common.by import By
- import time
- import random
-
-
- phone_input = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '//*[@id="mobilePhone" and not(@comps)]')))
- time.sleep(random.randint(1, 3)) # 随机等待一到三秒
- phone_input.send_keys("6466589337")
- time.sleep(2)
not(@comps)即代表定位不包含叫做comps属性的元素。
还可以用and或者or来指定多个属性值。
- //tr[not(@id) and not(@class)]
- 或者
- //tr[not(@id) or not(@class)]