我正在尝试使用selenium查找并单击此按钮:
<button class="orangeButton nextStep js-submitForm js-pop" data-href="/user/welcome/subscribe">Take me to my Subscriptions</button>
但是,没有id,而且类名太长,所以我想使用xpath。但是,我不知道如何使用它..
当前代码:
driver.FindElement(By.XPath("//button[@class='orangeButton nextStep js-submitForm js-pop\'")).Click();
但这会失败,因为它不是有效的xpath参数
发布于 2018-08-26 04:04:24
试一试。您不能将由空格分隔的类视为一个类字符串。空格表示它是一个不同的类,您必须分别对待每个类。
driver.FindElement(By.Xpath("//button[contains(@class, 'orangeButton') and contains(@class, 'nextStep') and contains(@class, 'js-submitForm') and contains(@class, 'js-pop')]"));
发布于 2018-08-26 04:08:21
不需要使用所有类来定位元素,只需要那些使元素唯一的类。
由css提供:
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和类:
driver.FindElement(By.XPath("//button[contains(@class,'orangeButton) and contains(@class,'nextStep)'"));
通过xpath和文本:
driver.FindElement(By.Xpath("//button[normalize-space(., 'Take me to my Subscriptions')"));
发布于 2018-08-27 15:29:54
没有。在您可以尝试使用xpath的方法中,只需尝试下面的xpath表达式:
"//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
还请检查下面(是否正常工作)
"//*[contains(@data-href,'/user/welcome/subscribe')]"
"//*[contains(@data-href,'subscribe')]"
使用AND、and或or
"//*[@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']"
https://stackoverflow.com/questions/52020510
复制相似问题