问题
I have the following XML structure:
<array-list>
<tests>
<test myId="11">Some data</test>
<test myId="22">Some data 2</test>
</tests>
</array-list>
How can I store "myId"
attribute of the first element in the list?
I have tried storeAttribute
command with the following location parameters with no luck:
1. //array-list/tests/test[1]@myId
2. xpath=//array-list/tests/test[1]@myId
3. */test[1]@myId
4. //array-list/tests/test@myId
5. //array-list/tests/test[1]/@myId
6. //array-list/tests/test/@myId
I believe there were some other attempts as well but none of them are working. Please do not say "correct XML". XML does not have any other attributes. That is retrieved from external service which I cannot modify. So the only question is how to get **myId**
attribute from the fist test tag in the list
回答1:
You will not be happy if I say to correct xml but You have to change myID to myid(all in small)(I am still in search for the reason why?).
I tried with the given xml
<array-list>
<tests>
<test myid="11">Some data</test>
<test myid="22">Some data 2</test>
</tests>
</array-list>
The code I tried is
<tr>
<td>storeAttribute</td>
<td>//test[1]/@myid</td>
<td>myVar</td>
</tr>
<tr>
<td>echo</td>
<td>${myVar}</td>
<td></td>
</tr>
and it worked well.
回答2:
So for example using css selector assuming the following changes:
<array-list>
<tests>
<test id="11">Some data</test>
<test id="22">Some data 2<img src="some attribute"></test>
</tests>
</array-list>
We could do something like this:
And the html:
<tr>
<td>storeText</td>
<td>css=test#11 </td>
<td>var</td>
</tr>
<tr>
<td>echo</td>
<td>${var}</td>
<td></td>
</tr>
<tr>
<td>storeAttribute</td>
<td>css=test#22 > img@src</td>
<td>img</td>
</tr>
<tr>
<td>echo</td>
<td>${img}</td>
<td></td>
</tr>
And the webdriver python version:
var = driver.find_element_by_css_selector("test#11").text
print(var)
img = driver.find_element_by_css_selector("test#22 > img").get_attribute("src")
print(img)
来源:https://stackoverflow.com/questions/13637094/cannot-store-attribute-using-selenium-ide