Nacht und Nebel
Nacht und Nebel

Reputation: 1

How Do You Select VALUES using Xpath's starts-with?

<option value="Employee19343">Person1</option> <br />
<option value="Other19343">Person2</option> <br />

. .

I want the value that starts with "Employee"!

This doesn't work - invalid syntax

value = tree.xpath('//option/@value[starts-with(.,'Employee')]')

Upvotes: 0

Views: 324

Answers (2)

Andersson
Andersson

Reputation: 52695

You got SyntaxError exception because of inconsistent usage of single and double quotes

Just try to mix single/double quotes as below

value = tree.xpath("//option/@value[starts-with(.,'Employee')]")

Upvotes: 1

Ganesh Pandhere
Ganesh Pandhere

Reputation: 1652

Try using below selector to get the exact option

//option[starts-with(@value, 'Employee')]

This will return expected options

So the code should look like

value = tree.xpath('//option[starts-with(@value, "Employee")]/@value')

Upvotes: 1

Related Questions