Python Selenium通过Xpath查找第二个元素
我想在python selenium中通过xpath(“//button[@class='sqdOP']”)查找元素,但是我有两个按钮,这是类,我需要第二个按钮,所以只需要第一个按钮。Python Selenium通过Xpath查找第二个元素,python,selenium,xpath,Python,Selenium,Xpath,我想在python selenium中通过xpath(“//button[@class='sqdOP']”)查找元素,但是我有两个按钮,这是类,我需要第二个按钮,所以只需要第一个按钮。 我试过下面的代码 find_element_by_xpath("//button[@class='sqdOP'][2]") find_element_by_xpath("//[button[@class='sqdOP']][2]") find_element_by_xp
我试过下面的代码
find_element_by_xpath("//button[@class='sqdOP'][2]")
find_element_by_xpath("//[button[@class='sqdOP']][2]")
find_element_by_xpath("//button[2][@class='sqdOP']")
find_element_by_xpath("//button[@class='sqdOP'[2]]")
上述操作均无效。使用
find\u elements\u by\u xpath
而不是find\u elements\u by\u xpath
来获取元素列表。访问此列表中的特定索引以获取所需元素。您可能正在查找此XPath表达式:
find_element_by_xpath("(//button[@class='sqdOP'])[2]"
只需添加()
即可获得页面上满足特定属性条件的第二个按钮
元素(@class='sqdOP'
)
旁注:您的第二次尝试(
/[button
..)不是有效的XPath表达式。您不能用谓词启动表达式。您需要元素名或通配符(*
)。您可以使用JavaScript executor进行尝试
WebElement buttton = driver.find_element_by_class_name("sqdOP")
driver.execute_script("arguments[1].click();",buttton)
参数[1]表示单击第二个元素