【问题标题】:find element in selenium by sub string通过子字符串在 selenium 中查找元素
【发布时间】:2021-07-01 11:34:47
【问题描述】:

我正在用 selenium 编写 java 代码,但我被卡住了。 我必须通过ID来查找元素,元素的Id是这样的"msi_num_create-copy"(num每次都在变化),有没有办法通过部分Id找到元素?

【问题讨论】:

  • //*[contains(@id, 'your_sub_string')] ,你也有函数starts-with,而不是contains

标签: java selenium selenium-webdriver findelement


【解决方案1】:

是的,您需要使用 css 选择器或 xpath :

XPATH 将是:

//*[contains(@id, '_create-copy') and starts-with(@id, 'msi')]

并像这样使用它:

WebElement someEle = driver.findElement(By.xpath("//*[contains(@id, '_create-copy') and starts-with(@id, 'msi')]"));
someEle.click(); or someEle.sendKeys("some string");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-12
    • 2017-08-08
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2014-07-25
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多