xpath - How to find an element by using DOM structure in Selenium? -


my application integrates google maps , i'm trying write ui tests using selenium. part of 1 of tests, need find x in infopopup provided google maps. element not have id or class can use directly find in selenium. can use xpath make test brittle changes google.

on other hand, can walk dom structure custom elements (use parent , sibling) , find (this how accessing in javascript). question whether can access same way in selenium.

html form:

<div style="width: 10px; height: 10px; overflow: hidden; position: absolute; opacity: 0.7; right: 12px; top: 12px; z-index: 10000; cursor: pointer;">        <img src="http://maps.gstatic.com/mapfiles/mv/imgs8.png" draggable="false" style="position: absolute; left: -18px; top: -44px; width: 68px; height: 67px; -webkit-user-select: none; border: 0px; padding: 0px; margin: 0px;"> </div> 

xpath: //*[@id="map"]/div[1]/div[1]/div/div[3]/div[3]/div/div[13]/div[1]

i have limited experience selenium. if there other ways of finding elements might me, love learn them well.

you may use by.xpath:

driver.findelement(by.xpath("//*[@id='map']/div[1]/div[1]/div/div[3]/div[3]/div/div[13]/div[1]")); 

however, xpath highly unstable , hard maintain, don't recommend so.

use meaningful xpath or cssselector nice.

by.xpath("//*[@id='map']//div[./img[contains(@src, 'mapfiles/mv/imgs8.png')]]"); by.cssselector("#map img[src$='mapfiles/mv/imgs8.png']");  

for more info xpath or cssselector, please refer the documentation.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -