我无法在以下代码的文章中检测到按钮:

<article id="ride-f6ba24ca-d847-44b7-987e-81db6e6dee47" class="DetailPage__container--1VLdd"><div class="DetailPage__highlights--1uyrQ"><section></section><form aria-label="Offer highlights" class="DetailPage__section--qtXxV"><button type="submit"><span>Accept offer</span></button></form></div></article>


我尝试:

driver.findElement(By.xpath("//*[text()='Details']"))
driver.findElement(By.xpath("//button[.//span[text()='Accept offer']]"))


没有运气

我在Java中无法检测到硒元素接受报价

最佳答案

期望的元素是动态元素,因此要定位必须为elementToBeClickable()引发WebDriverWait的元素,可以使用以下Locator Strategies之一:


cssSelector

WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("article[class^='DetailPage__container--'][id^='ride-']>div[class^='DetailPage__highlights--'] button[type='submit']>span")));

xpath

WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//article[starts-with(@class, 'DetailPage__container--') and starts-with(@id, 'ride-')]/div[starts-with(@class, 'DetailPage__highlights--')]//button[@type='submit']/span[text()='Accept offer']")));

09-25 22:17