:包含带有Selenium和Select2的选择器 [英] :contains selector with Selenium and Select2

查看:95
本文介绍了:包含带有Selenium和Select2的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Selenium仅支持浏览器支持的CSS选择器.在我的HTML应用程序中,我使用的是jQuery,但:contains(str)仍然向我抛出一个无效或非法的字符串, 指定的错误.

I know that Selenium only supports the CSS selectors that the browser supports. In my HTML app I'm using jQuery but :contains(str) is still throwing me an invalid or illegal string was specified error.

我正在将文本输入到Select2组件中,等待10秒钟,然后尝试选择结果.

I'm typing text into a Select2 component, waiting 10 seconds, then trying to select the result.

wd.findElement(By.id("s2id_autogen1")).sendKeys(p.getDisplayName());

// Wait 10 seconds

wd.findElement(By.cssSelector(".select2-result-label:contains('"+p.getDisplayName()+"')")).click();

为什么我仍然遇到该错误;有任何想法吗?谢谢!

Why am I still getting that error; any ideas? Thank you!

更新:不能使用xpath.必须使用CSS选择器来完成.

UPDATE: Using xpath is not an option. It must be done using CSS selectors.

推荐答案

使用xpath代替CSS:

Use xpath instead of css:

wd.findElement(By.xpath("//[@class='select2-result-label and contains(text(), '"+p.getDisplayName()+"')]")).click();

如果要避免使用xpath,可以改用以下方法:

If you're trying to avoid xpath, you can try this approach instead:

List<WebElement> elements = wd.findElements(By.cssSelector(".select2-result-label"));
WebElement temp;

foreach(WebElement element in elements)
{
    if(element.getText().contains(p.getDisplayName())
        temp = element;
}

这是伪代码,因为我不确定您使用的是什么.

It's psuedo-code, as I'm not entirely sure what you're using.

这篇关于:包含带有Selenium和Select2的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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