Any idea what I am doing wrong? I think my xpath might be wrong, but I am not sure.

Thanks!

Answer1:

There are three reasons why you are probably not being able to select the code you want.

1 - To select your class attribute in your XPath predicate you need to use the attribute axis. Either prefix the attribute name with attribute:: or with an @ sign. So you should use

@class

to select the class attribute.

2 - An XPath expression is made of one or more steps. Each step defines a context that limits the scope of the next step. The last step contains the set you are selecting. Since your last step is a div, you are actually selecting a div, and not an a. You should use the following expression to select the a node and its contents:

//div[@class="ProductImage QuickView"]/a

3 - Finally, your page has a default namespace declaration:

xmlns="http://www.w3.org/1999/xhtml"

That will require you to either register it or ignore it selecting your elements using wildcards (not by their names, but using *). Most XPath APIs do not automatically set default namespaces, and if a namespace is not used to qualify XPath selectors, it considers unprefixed elements as belonging to no namespaces. That means that if you try to select a <div> using the expression //div, you may get an empty set. If you are not selecting anything, try ignoring namespaces like this:

Contact Us

No Copyright Statement

This site is a non-profit exchange learning website. All resources are collected online. Copyright belongs to its copyright owner. This site does not enjoy copyright. If you think it is harmful to your copyright, please contact us and we will delete it at the first time.

Statement of Compliance with the Law

The information collected in this website does not mean that XSZZ. ORG agrees with its statement or description, nor does it constitute any suggestion. It is only for the study and reference of interested parties. If you need to use it, you must abide by the provisions of Chinese law.