如何在硒中找到具有多个类的元素 [英] How to find element that has multiple class in selenium

查看:38
本文介绍了如何在硒中找到具有多个类的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 3 个类的元素,我需要用 selenium 找到它

I have an element with 3 classes which I need to find with selenium

<button style="padding:2px 7px; background-color:#4caeea" 
  class="btn btn-xs btn-custom" </button>

我找不到它:By.classname("btn btn-xs btn-custom")

我不想使用 xpath &cssSelector .我还有什么其他选择?

I do not want to use xpath & cssSelector . What other options do I have ?

推荐答案

这个 By.classname("btn btn-xs btn-custom") 将不起作用,因为它包含多个空格,这意味着它是 3 个类的组合.

This By.classname("btn btn-xs btn-custom") will not work, as it contains multiple spaces which means it is combination of 3 classes.

你将不得不切换到 css selector 或 xpath ,我不知道你为什么提到你不想同时使用它们.

You will have to switch to css selector or xpath , I do not know why you have mentioned that you do not want to use both of them.

但是,如果您有兴趣使用 css 选择器:

However, If you are interest to use css selector :

你可以试试这个:

By.cssSelector("btn.btn-xs.btn-custom")  

如果你按优先顺序去:

  1. ID
  2. 姓名
  3. 类名
  4. linkText
  5. partialLinkText
  6. 标签名称
  7. css 选择器
  8. xpath

这篇关于如何在硒中找到具有多个类的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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