首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何通过Selenium按照html点击按钮?

如何通过Selenium按照html点击按钮?
EN

Stack Overflow用户
提问于 2018-08-26 03:16:39
回答 3查看 98关注 0票数 -1

我正在尝试使用selenium查找并单击此按钮:

代码语言:javascript
复制
<button class="orangeButton nextStep js-submitForm js-pop" data-href="/user/welcome/subscribe">Take me to my Subscriptions</button>

但是,没有id,而且类名太长,所以我想使用xpath。但是,我不知道如何使用它..

当前代码:

代码语言:javascript
复制
driver.FindElement(By.XPath("//button[@class='orangeButton nextStep js-submitForm js-pop\'")).Click();

但这会失败,因为它不是有效的xpath参数

EN

回答 3

Stack Overflow用户

发布于 2018-08-26 04:04:24

试一试。您不能将由空格分隔的类视为一个类字符串。空格表示它是一个不同的类,您必须分别对待每个类。

代码语言:javascript
复制
driver.FindElement(By.Xpath("//button[contains(@class, 'orangeButton') and contains(@class, 'nextStep') and contains(@class, 'js-submitForm') and contains(@class, 'js-pop')]"));
票数 0
EN

Stack Overflow用户

发布于 2018-08-26 04:08:21

不需要使用所有类来定位元素,只需要那些使元素唯一的类。

由css提供:

代码语言:javascript
复制
driver.FindElement(By.CssSelector("button.nextStep"));
driver.FindElement(By.CssSelector("button.orangeButton.nextStep"));
driver.FindElement(By.CssSelector("button.orangeButton.nextStep.js-submitForm.js-pop"));

通过xpath和类:

代码语言:javascript
复制
driver.FindElement(By.XPath("//button[contains(@class,'orangeButton) and contains(@class,'nextStep)'"));

通过xpath和文本:

代码语言:javascript
复制
driver.FindElement(By.Xpath("//button[normalize-space(., 'Take me to my Subscriptions')"));
票数 0
EN

Stack Overflow用户

发布于 2018-08-27 15:29:54

没有。在您可以尝试使用xpath的方法中,只需尝试下面的xpath表达式:

代码语言:javascript
复制
"//button[@class='orangeButton nextStep js-submitForm js-pop']"
"//*[@class='orangeButton nextStep js-submitForm js-pop']"
"//*[contains(@class,'orangeButton nextStep js-submitForm js-pop')]"
"//*[contains(@class,'orangeButton')]" // contains() can accept partial text as well

还请检查下面(是否正常工作)

代码语言:javascript
复制
"//*[contains(@data-href,'/user/welcome/subscribe')]"
"//*[contains(@data-href,'subscribe')]"

使用AND、and或or

代码语言:javascript
复制
"//*[@class='orangeButton nextStep js-submitForm js-pop' and @data-href='/user/welcome/subscribe']"
"//*[@class='orangeButton nextStep js-submitForm js-pop' OR @data-href='/user/welcome/subscribe']"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52020510

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档