如何将字符串传递到包含文本的XPath? [英] How to pass string to an XPath containing text?

查看:87
本文介绍了如何将字符串传递到包含文本的XPath?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过使用以下代码,我使用Selenium Webdriver获得处于选定模式的文本的ID:

I get id of a text which is in selected mode using Selenium Webdriver by using this code:

String requiredId = driver.FindElement(By.XPath("//option[@selected='selected' and .='Blue']/..")).GetAttribute("id");

String requiredId = driver.FindElement(By.XPath("//option[@selected='selected' and .='Blue']/..")).GetAttribute("id");

如何传递字符串getColour代替蓝色?

How can i pass string getColourin place of Blue?

谢谢

推荐答案

您可以通过这种方式传递字符串.尝试下面的代码

You can pass a string this way. Try below code

    string getColourin = "Red";
    String requiredId = driver.FindElement(By.XPath("//option[@selected='selected' and .='" + getColourin +"']/..")).GetAttribute("id");

OR

使用字符串.格式

string xpathBefore = "//option[@selected='selected' and .='{0}']/..";
string getColourin = "Red";
string finalXpath = string.Format(xpathBefore, getColourin);

String requiredId = driver.FindElement(By.XPath(finalXpath)).GetAttribute("id");

这篇关于如何将字符串传递到包含文本的XPath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆