点击按钮使用Selenium WebDriver Java

huangapple go评论139阅读模式
英文:

Clicking buttons with Selenium WebDriver Java

问题

XPath:

/html/body/div[1]/div/div/form/div[21]/div[3]/button

[HTML]

点击按钮使用Selenium WebDriver Java

英文:

There is a button on a website I am testing and Selenium can't seem to click it. I have tried many different ways but still nothing. Attached is the HTML for the button as well as the XPath.
Has anyone else experienced this or knows how to get around this?

XPath:

/html/body/div[1]/div/div/form/div[21]/div[3]/button

[HTML]

点击按钮使用Selenium WebDriver Java

答案1

得分: 0

尝试以下代码 -

Actions action = new Actions(driver);
WebElement My_btn = webdriver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
action.moveToElement(My_btn).click(My_btn).build().perform();

让我知道结果。

英文:

Try below code -

Actions action = new Actions(driver);
WebElement My_btn = webdriver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
action.moveToElement(My_btn).click(My_btn).build().perform();

Let me know the outcome.

答案2

得分: 0

可能是同步问题。如果您的元素不在 iframe 中,可以尝试以下解决方案,否则您需要在处理网页元素之前切换到 iframe 控制。

WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"))).click();

或者您也可以尝试 JavaScript 解决方案:

WebElement element = driver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
英文:

Could be a synchronization issue. kindly try below solution if your element is not within iframe or else you need to switch control to iframe first before dealing with the web element.

WebDriverWait wait = new WebDriverWait(driver, 30);  
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"))).click();

Or You can also try javascript solution:

WebElement element= driver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);

huangapple
  • 本文由 发表于 2020年7月29日 00:18:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63138529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定