7Gabriel / selenium-vba

Automatically exported from code.google.com/p/selenium-vba
0 stars 0 forks source link

Find correct Element with Different Elements Having same ID #143

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Operating system :Win 7 64bit
.Net Framework version :
Office Version : 2007
SeleniumWrapper version : 1.0.19.

I am new to HTML, CSS, Xpath, etc but pretty good at VBA.
I am trying to fetch the price of specific products at newegg.com, and track 
them in my spreadsheet. My problem is finding a reliable way of identifying the 
correct place for the price. I have attached a picture of the code for a sample 
newegg product:
Inline image 1

As you can see from the highlighted text I can find the CSS Selector 
(#singleFinalPrice) and I guess I could use innerHTML to parse the price out :

findElementByCssSelector("#singleFinalPrice").getAttribute("innerHTML")

but there are other parts in the page that have the same CSS Selector with 
totally different prices:
Inline image 2

I guess my question to you is, How can I just get the "content" attribute from 
the first picture into a variable in VBA

<li class="price-current " itemprop="price" content="169.99" 
id="singleFinalPrice">

Your help is greatly appreciated.
thanks,

Original issue reported on code.google.com by and...@thefigueroas.info on 13 Mar 2015 at 10:20

Attachments:

GoogleCodeExporter commented 8 years ago
Ids are required to be unique. Having doubles kind of breaks the purpose of an 
id and usually reflects a very poor implementation of the website.
It's still possible to deal with it, either by index or by making sure an 
attribute is present:
value = 
driver.findElementByCssSelector("#singleFinalPrice[content]").getAttribute("cont
ent")
or :
value = 
driver.findElementByCssSelector("#singleFinalPrice:nth-child(0)").getAttribute("
content")

Original comment by florentbr on 16 Mar 2015 at 10:52