这是我写的代码。
public WebDriver createPart() {
try {
driver.findElement(By.id("username")).sendKeys("502409373");
driver.findElement(By.id("password")).sendKeys("Magic14Magic");
driver.findElement(By.id("submitFrmShared")).click();
//Thread.sleep(10000);
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
Select dropCountry = new Select(driver.findElement(By.id("txtNewLocation")));
dropCountry.selectByVisibleText("India");
driver.findElement(By.xpath("//button[@class='btn']/label")).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
//driver.findElement(By.xpath("//span[@class='ds-coachmark-close']")).click();
//driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
//driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
Thread.sleep(10000);
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
driver.findElement(By.xpath("//li[@class='icon-button add']/span")).click();
driver.findElement(By.xpath("//div[@id='ENCActions']/a/label")).click();
driver.findElement(By.xpath("//label[starts-with(text(),'Create Part...')]")).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
System.out.println("k1");
}
System.out.println(driver.switchTo().window(subWindowHandler).getTitle());
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = driver.findElement(By.xpath("//select[@id='Type-Field']//following-sibling::div//div//input"));
element.click();
Thread.sleep(3000);
element.sendKeys(Keys.BACK_SPACE);
Thread.sleep(3000);
element.sendKeys("Subassy");
Thread.sleep(4000);
driver.findElement(By.xpath("//div[@data-value='Subassy']")).click();
driver.findElement(By.xpath("//span[text()='Description']//parent::td//following-sibling::td//textarea")).sendKeys("Testing");
driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']//parent::select")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//option[text()='BioSc-DS-Chemical']")).click();
driver.findElement(By.xpath("//a[text()='Done']")).click();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
System.out.println(driver.switchTo().window(parentWindowHandler).getTitle());
driver.switchTo().frame("content");
driver.switchTo().frame("detailsDisplay");
driver.findElement(By.xpath("//a[@title='Part Details']")).click();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return driver;
}
这是堆栈跟踪:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no
such element: Unable to locate element:
{"method":"xpath","selector":"//a[@title='Part Details']"}
(Session info: chrome=66.0.3359.181)
(Driver info: chromedriver=2.38.552522
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.1.7601 SP1
x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 70.04 seconds
For documentation on this error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: 'unknown', revision: '5234b32', time: '2017-03-10
09:00:17 -0800'
System info: host: 'TMIC-PCS2200', ip: '3.235.34.200', os.name: 'Windows 7',
os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false,
mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome=
{chromedriverVersion=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),
userDataDir=C:\Users\KT0047~1\AppData\Local\Temp\scoped_dir14184_21633},
takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false,
handlesAlerts=true, hasTouchScreen=false, version=66.0.3359.181,
platform=XP, browserConnectionEnabled=false, nativeEvents=true,
acceptSslCerts=false, acceptInsecureCerts=false,
locationContextEnabled=true, webStorageEnabled=true, browserName=chrome,
takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true,
setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: 818a330c1e7b24086d7d06935e830e51
*** Element info: {Using=xpath, value=//a[@title='Part Details']}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
我附上了html代码的截图。Html code screenshot
在此代码中,我试图点击“零件详情”&#39;内部链接&details;详细显示&#39;框架和此框架位于另一个框架内容&#39;。我无法找到&#39;部件详细信息&#39;内部链接元素&details 39
答案 0 :(得分:0)
您可以等待帧完全加载,如下所示。它可以解决你的问题。
WebDriverWait wait = new WebDriverWait(driver,60);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("content");
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("detailsDisplay");
driver.findElement(By.xpath("//a[@title='Part Details']")).click();
答案 1 :(得分:0)
正如我所看到的,页面顶部有一个iframe元素,用于绑定要与之交互的元素。也许你可以尝试找到&#34; Part Details&#34;像这样的链接:
driver.switchTo().frame(driver.findElement(By.xpath("//a[@title='Part Details']")));
修改强>:
似乎你仍处于外部框架,这就是为什么selenium总是抛出NoElementException。我有一个想法,也许您可以尝试找出iframe是否实际包含该元素。
// we find all iframes
int size = webDriver.findElements(By.tagName("iframe")).size();
// we loop through all iframes to search for Part Details link
for(int i=0; i<size; i++) {
webDriver.switchTo().frame(i);
int total = webDriver.findElements(By.cssSelector("a[title*='Part Details']")).size();
System.out.println(total);
webDriver.switchTo().defaultContent();
}
我们使用webDriver.findElements
来避免NoElementException
,如果webDriver可以找到元素,则total将为1,否则为0.如果这样做,则可以继续进行。我希望它有所帮助:)
答案 2 :(得分:0)
当您提出这样的问题时,请不要插入html代码的屏幕截图。插入源。对于想要回答的人来说,尝试提供帮助会更容易。
切换到iframe "detailsDisplay"
后,尝试使用以下命令找到您的元素:
driver.findElement(By.xpath("//div[@id='tvcTabs0']/div[@class='tabContainer']/ul/li/div[@class='tabcontent]/a[@title='Part Details']")).click();
我没有测试它,因为我使用屏幕截图创建了它。如果您已插入源,我可以进行测试。