2016-08-25 41 views
1

我試圖在CQ5網頁中找到一個鏈接並點擊它。Selenium:當它不會有絕對路徑時,如何點擊href鏈接

下面是html代碼:

<a href="/siteadmin" title="Websites"> 
<div class="icon siteadmin"> 
<div class="title">Websites</div> 
<div class="description">Create and manage multiple websites. 
</div> 
</div> 
</a> 

下面是方法我試圖找到所需的信息,並點擊它。

driver.findElement(By.linkText("Websites")).click(); 
driver.findElement(By.xpath("//a[@href='/siteadmin']")).click(); 

獲得以下異常:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (528, 174). Other element would receive the click: <div class="resourcebox">...</div> (Session info: chrome=52.0.2743.116) (Driver info: chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 39 milliseconds Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:32:46' System info: host: 'pilapwin7020', ip: '10.116.252.187', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_101' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b), userDataDir=C:\Users\User\AppData\Local\Temp\scoped_dir44444_30971}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=52.0.2743.116, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 5116ba44126c39293ac466aed3d6608a

回答

0

你應該嘗試使用JavascriptExecutor在這種情況下進行點擊如下: -

((JavascriptExecutor)driver).executeScript("arguments[0].click()", driver.findElement(By.cssSelector("a[href = '/siteadmin'][title = 'Websites']"))); 
0

您需要檢查ATTR包含字符串,因爲即使在HTML中只顯示/siteadmin,它也會返回完整路徑。

XPath的解決方案:

//a[contains(@href, '/siteadmin') and @title='Websites'] 

CSS選擇器解決方案:

a[href*='/siteadmin'][title='Websites']