我使用 Selenium Webdriver 用 Ja??va 撰寫了以下代碼,但它沒有單擊帶有星級的 div
driver.get("https://goo.gl/maps/gLCX3PitJT1cXr9v9");
driver.findElement(By.xpath("//button[@data-value=\"Escribir una opinión\"]")).click();
driver.switchTo().frame(2);
driver.findElement(By.xpath("//textarea")).sendKeys("This is just a test");
driver.findElement(By.xpath("//span[@aria-label=\"Cuatro estrellas\"]")).click();
在每一行之間,我還添加了一個 Thread.sleep(5000) 以確保頁面完全加載。
我看到的唯一明確的識別符號是 aria-label。

手動步驟:
- 打開網址:
uj5u.com熱心網友回復:
要在星級上單擊()4星,因為所需的元素在 a 內,
<iframe>因此您必須:- 誘導WebDriverWait用于期望frameToBeAvailableAndSwitchToIt。
- 為所需的elementToBeClickable引入WebDriverWait。
- 您可以使用以下任一定位器策略:
使用cssSelector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe.goog-reviews-write-widget"))); new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[aria-label='Four stars']"))).click();使用xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@class='goog-reviews-write-widget']"))); new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@aria-label='Four stars']"))).click();
參考
您可以在以下位置找到一些相關討論:
- iframe下#document的處理方式
- 在 Selenium Webdriver Java 中,是否可以在不使用 driver.switchTo().frame(“frameName”) 的情況下切換到框架中的元素?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/376132.html上一篇:SeleniumPython:嘗試在iframe中獲取iframe
下一篇:PythonSelenium:將結果列印為[<selenium.webdriver.remote.webelement.WebElement(session="9dc768d9ede7
