【问题标题】:Is there a way to identify the elements having 3 classes? [closed]有没有办法识别具有 3 个类的元素? [关闭]
【发布时间】:2020-08-24 11:34:46
【问题描述】:

我遇到了一个元素有 3 个类的情况,我需要获取名为“data-source”的属性值并且应该获取该值。

谁能帮我解决这个问题?

提前致谢

【问题讨论】:

  • 请在这里分享 HTML 元素?
  • 请提供您的 HTML Dom 代码

标签: selenium selenium-webdriver xpath css-selectors getattribute


【解决方案1】:

获取具有 3 个类的元素的属性 data-source,例如classAclassBclassC 你可以使用你可以使用以下任一Locator Strategies

  • 使用cssSelector

    System.out.println(driver.findElement(By.cssSelector(".classA.clasB.classC")).getAttribute("data-source"));
    
  • 使用xpath

    print(driver.find_element_by_xpath("//*[@class='classA classB classC']").get_attribute("data-source"))
    

理想情况下,点击你需要为element_to_be_clickable()诱导WebDriverWait的元素,你可以使用以下Locator Strategies之一:

  • 使用cssSelector

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".classA.clasB.classC"))).getAttribute("data-source"));
    
  • 使用xpath

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//*[@class='classA classB classC']"))).get_attribute("data-source"))
    
  • 注意:对于 Python,您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

【讨论】:

  • 嗨,Debanjan,感谢您的快速回复,但是在 python 中使用它时,它只给我抛出 None 值
  • @Sunny 查看更新的答案并告诉我状态。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-05
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 2021-06-19
  • 2017-10-25
  • 2020-01-25
相关资源
最近更新 更多