Pra*_*ams 2 selenium selenium-webdriver
例如,考虑以下html标记,我需要在其中获取文本的确切索引/位置,three
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
期望值为“ 3 ”
您可以这样做:
from selenium import webdriver
driver = webdriver.Firefox()
# This is an actual bin with a test page in it.
driver.get("http://jsbin.com/wagonazipa")
# Obviously, this needs to be precise enough to find the tr
# you care about. Adapt as needed.
tr = driver.find_element_by_tag_name("tr")
# Find the element you care about. You might want to use td rather
# than *.
target = tr.find_element_by_xpath("*[. = 'three']")
# Get all the children of the row.
children = tr.find_elements_by_xpath("*")
# Get the index of target in the children list.
print children.index(target)
Run Code Online (Sandbox Code Playgroud)
Python对Selenium的实现使您可以使用进行WebElement对象之间的比较==,从而可以进行index工作。如果您使用的语言无法做到这一点,则必须获取Selenium分配给每个WebElement对象的标识符。在Python上,这就是.id属性。在其他语言中,您可以使用getId()or或get_id()方法来获取ID。然后,您可以WebElement按标识符比较对象。
如果无法访问jsbin,则这是其中的相关HTML:
<body>
<table>
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
</tr>
</table>
</body>
Run Code Online (Sandbox Code Playgroud)
这是相同使用的示例JAVA,
driver.get("http://www.indiabookstore.net/");
WebElement list = driver.findElement(By.xpath("//ul[@class='nav navbar-nav navbar-right']"));
WebElement li = list.findElement(By.xpath("*[. = 'Offers']"));
List<WebElement> children = driver.findElements(By.tagName("li"));
System.out.println(children.indexOf(li));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8637 次 |
| 最近记录: |