2016-02-16 33 views
0
<ul class="popupmenu is-open" id="menuFormDefaultFC" role="menu" aria-hidden="false"> 
<li role="presentation"><a tabindex="-1" role="menuitem" href="#A">Add</a> 

請有人讓我知道上面的html的Xpath與href在它。來自HTML的XPATH標識符

回答

1

下面是HREF幾個選項。

1. By.xpath(".//a[@href='#A']"); 
2. By.xpath(".//li[@role='presentation']/a[@href='#A']"); 

但是,對於鏈接,你可以使用只是linkText。除非您的應用程序支持多個區域設置,否則我更喜歡以下內容

By.linkText("Add"); 

或者使用CSS選擇器

1. By.cssSelector("a[role='presentation']"); 
2. By.cssSelector("li[role='presentation']>a[href='#A']"); 
0

試試這個//a[contains(text(),'Add')]

它可以用作driver.find_element_by_xpath("//a[contains(text(),'Add')]")

0

我想這樣做:

By closedMenu = By.xpath(".//ul[not(contains(@class, 'is-open'))]"); 
By openMenu = By.xpath(".//ul[contains(@class, 'is-open')]"); 
By addItem = By.linkText("Add"); 
By addItemLocator = new ByChained(openMenu, addItem);