我正在试图弄清楚如何遍历网站上的几个屏幕并使用Selenium(首次尝试使用它)和目前的firefox webdriver检索数据。我有时会工作,而不是其他工作,我得到了很好的结果。我开始在firefox中使用Selenium IDE并记录我想要做的事情。我用它作为我下面java程序的基础。在导出时出现此错误:“// ERROR:Caught exception [错误:不支持的命令[selectFrame | ghinwidget_iFrame_0 |]]”。我将其转换为:“driver.switchTo()。frame(”ghinwidget_iFrame_0“);”。那失败了,不知怎的,我得到了“driver.switchTo()。frame(”ghinwidget_iFrame_3“);”工作,但现在无法弄清楚我在哪里... _ iFrame_3。这一切现在大部分时间都有效,但经常在“waitOnID(”ghinwidget_iFrame_3“,20)上失败;”在超时。如果我切换到firefox,它正是我期望等待GHIN编号的地方:输入。
My sequence:
Go to www.ghin.com
Click on Handicap Lookup
This is where my java program will fail if it is going to
Enter GHIN number: 1234567
Press "Lookup" button
Data I am expecting and get most of the time.
任何人都可以解释我的不一致吗?
package org.ghinlookup;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class GHINLookup
{
private static WebDriver driver;
private static String baseUrl;
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver",
"/Users/myuser/Desktop/Desktop Archive/Crashplan/Gecko/geckodriver");
driver = new FirefoxDriver();
baseUrl = "http://www.ghin.com/";
driver.get(baseUrl + "/");
System.out.println("1Page title is: " + driver.getTitle());
driver.findElement(By.cssSelector("b")).click();
waitOnID("ghinwidget_iFrame_3", 20);
System.out.println("2Page title is: " + driver.getTitle());
// ERROR: Caught exception [ERROR: Unsupported command [selectFrame | ghinwidget_iFrame_0 | ]]
driver.switchTo().frame("ghinwidget_iFrame_3");
waitOnID("ctl00_bodyMP_tcLookupModes_tpSingle_tbGHIN", 2);
driver.findElement(By.id("ctl00_bodyMP_tcLookupModes_tpSingle_tbGHIN")).clear();
waitOnID("ctl00_bodyMP_tcLookupModes_tpSingle_tbGHIN", 2);
driver.findElement(By.id("ctl00_bodyMP_tcLookupModes_tpSingle_tbGHIN")).sendKeys("1234567");
waitOnID("ctl00_bodyMP_tcLookupModes_tpSingle_btnSubmit1", 2);
driver.findElement(By.id("ctl00_bodyMP_tcLookupModes_tpSingle_btnSubmit1")).click();
waitOnID("__tab_ctl00_bodyMP_tcItems_tpMostRecent", 2);
driver.findElement(By.id("__tab_ctl00_bodyMP_tcItems_tpMostRecent")).click();
// driver.switchTo().frame("ghinwidget_iFrame_0");
// waitOnID("ctl00_bodyMP_tcItems_tpMostRecent_slMostRecent_grdScores", 20);
WebElement recentScores = driver.findElement(By.id("ctl00_bodyMP_tcItems_tpMostRecent_slMostRecent_grdScores"));
String text = recentScores.getText();
System.out.println(text);
driver.quit();
}
private static void waitOnID(String elementID, int secs)
{
System.out.println("Waiting on : " + elementID);
WebDriverWait wait = new WebDriverWait(driver, secs);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(elementID)));
System.out.println("Got : " + elementID);
}
private static void sleepFor(int secs)
{
try
{
Thread.sleep(1000 * secs); // Wait 2 seconds
}
catch (InterruptedException ex)
{
Logger.getLogger(GHINLookup.class.getName()).log(Level.SEVERE, null, ex);
}
}
}