如何通过Selenium和Python使用classname属性定位最后一个web元素

如何通过Selenium和Python使用classname属性定位最后一个web元素,python,selenium,selenium-webdriver,xpath,getattribute,Python,Selenium,Selenium Webdriver,Xpath,Getattribute,不工作,我需要得到字符串格式的标题 getphone = driver.find_element_by_class_name('_3ko75')[-1] phone = getphone.get_attribute("title") 根据您的代码试用,要根据classname属性的值获取最后一个代码的标题,可以使用以下任一选项: 使用XPATH,find_元素*和last(): 使用XPATH,查找元素*和[-1]: print(driver.find_element_

不工作,我需要得到字符串格式的标题

getphone = driver.find_element_by_class_name('_3ko75')[-1]
phone = getphone.get_attribute("title")

根据您的代码试用,要根据classname属性的值获取最后一个代码的标题,可以使用以下任一选项:

  • 使用
    XPATH
    find_元素*
    last()

  • 使用
    XPATH
    查找元素*
    [-1]

    print(driver.find_element_by_xpath("//*[@class='_3ko75'][last()]").get_attribute("title"))
    
最好使用:


根据您的代码试用,要根据classname属性的值获取最后一个代码的标题,可以使用以下任一选项:

  • 使用
    XPATH
    find_元素*
    last()

  • 使用
    XPATH
    查找元素*
    [-1]

    print(driver.find_element_by_xpath("//*[@class='_3ko75'][last()]").get_attribute("title"))
    
最好使用:


@andrea仍然不工作:getphone=driver.find_element_by_class_name(“'u 3ko75')[-1]phone=getphone.get_属性(“标题”)我只是编辑了你的问题,以便更好地组织代码。我没有提供任何答案Thank you andrafind_元素只返回一个WebElement对象。为什么在结尾使用[-1]?@andrea仍然不起作用:getphone=driver。通过类名称(“3ko75”)查找元素[-1]phone=getphone。获取属性(“标题”)我只是编辑了你的问题,以便更好地组织代码。我没有提供任何答案Thank you andrafind_元素只返回一个WebElement对象。你为什么在最后使用[-1]?@Victormahato很高兴能帮助你。为了未来读者的利益,如果这个/任何答案对您有帮助,@Victormahato很高兴能够帮助您。如果这个/任何答案对你有帮助,对未来的读者有好处。
print(driver.find_elements_by_xpath("//*[@class='_3ko75']")[-1].get_attribute("title"))
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='_3ko75'][last()]"))).get_attribute("title"))
print(WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//*[@class='_3ko75']")))[-1].get_attribute("title"))