Joyce
Joyce

Reputation: 435

Python selenium xpath using contains and not contains

I try to get links whose title contains some word in the mean time not contains some words, I use the following code but it says is not a valid XPath expression.

Please find my code here:

Any help will be highly appreciated!

driver.get("http://www.csisc.cn/zbscbzw/isinbm/index_list_code.shtml")
while True:
    links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("//a[(contains(@title,'公司债券')and not(contains(@title,'短期'))]")]
    for link in links:
               driver.get(link)
               #dosth

Upvotes: 1

Views: 3411

Answers (2)

Merolla
Merolla

Reputation: 315

There is an extra bracket in you xpath, use

links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("//a[contains(@title,'公司债券')and not(contains(@title,'短期'))]")] 

instead You can use chrome developer tools first to validate your xpaths enter image description here PS: I changed the xpath here a bit to be able to find some elements in my page

Upvotes: 4

JaSON
JaSON

Reputation: 4869

There should be space before and. Also there is extra leading bracket in your XPath. Try:

"//a[contains(@title,'公司债券') and not(contains(@title,'短期'))]"

Upvotes: 1

Related Questions