In Visual Studio writing the code for Selenium WebDriver, these two codes for the same button work fine only once.
Click the button By Css Selector:
driver.FindElement(By.CssSelector(".follow-text")).Click();
Click the button By XPath:
driver.FindElement(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']")).Click();
Until this all correct...
But I want to click to all the buttons not to only the first, and because of the FindElements
(in plural) get me error, how can I press click to all the buttons with that same code?
Using this get error:
List<IWebElement> textfields = new List<IWebElement>();
driver.FindElement(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']")).Click();
driver.FindElement(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn'][3]")).Click();
See the capture:
You need to loop through FindElements
result and call .Click()
on each item :
var result = driver.FindElements(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']"));
foreach (IWebElement element in result)
{
element.Click();
}
FYI, you need to wrap the XPath in brackets to make your attempted code using XPath index works :
driver.FindElement(By.XPath("(//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn'])[3]")).Click();
List <WebElement> list = driver.FindElements(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']"));
And then iterate over the list of elements contained in the list:
int x = 0;
while (x < list.size()) {
WebElement element = list.get(x);
element.click();
}
You should use something like that (note the s in findElements)
List<WebElement> textfields = driver.findElements(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']"));
and then iterate with a for loop
for(WebElement elem : textfields){
elem.click();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.