gpt4 book ai didi

java - 无法单击 Selenium Webdriver 上的按钮(元素)

转载 作者:行者123 更新时间:2023-12-01 09:33:26 25 4
gpt4 key购买 nike

enter image description here无法单击 Selenium Webdriver 上的按钮(元素)。它显示没有此类元素异常。

HTML:

<button id="datepicker-354-7412-title" class="btn btn-default btn-sm uib-title" tabindex="-1" ng-disabled="datepickerMode === maxMode" ng-click="toggleMode()" type="button" aria-atomic="true" aria-live="assertive" role="heading">
<strong class="ng-binding">August 2016</strong>
</button>

Java:

driver.findElement(By.xpath("//*[@id='flip-card']/div[2]/div/div[2]/div[2]/div[1]/div/div[2]/i")).click();
driver.manage().timeouts().implicitlyWait(05, TimeUnit.SECONDS);
//driver.findElement(By.xpath("//ul[@class='uib-datepicker-popup dropdown-menu ng-scope']/li/div/table/thead/tr/th/button[@id='datepicker-758-2620-title']/strong")).click();
//driver.findElement(By.xpath(".//*[@id='datepicker-961-3767-title']")).click();
//WebElement mm=driver.findElement(By.id("datepicker-1164-5186-title"));
//mm.click();
/*WebElement element=driver.findElement(By.xpath("//*[@id='datepicker-354-7412-title']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click()", element); */
driver.findElement(By.xpath("//button[@id='datepicker-354-7412-title']/strong")).click();

最佳答案

id 可能是动态的,尝试通过包含 datepickertitle 的部分 id 来定位按钮

driver.findElement(By.cssSelector("[id*='datepicker'][id*='title']")).click();

您还可以使用显式等待来确保按钮在单击之前存在/可见

WebDriverWait wait = new WebDriverWait(driver, 10);

// visible
WebElement button = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("[id*='datepicker'][id*='title']")));
button.click();

// or exist
WebElement button = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("[id*='datepicker'][id*='title']")));
button.click();

关于java - 无法单击 Selenium Webdriver 上的按钮(元素),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39204417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com