2

I'm looking for a solution with this small selenium script. The problem happens when on the list there are more than 1 occurence of the text. i'm looking for in with xpath command[contains(). Then script stops.

As you see on my script tried to use css selector (un-commented) but it not valid. I have seen some solution with regular expression with css selector with ^ and $ but i don't how it works.

EDIT: I'm looking to select only the third element "LIT" as you see there are thee times LIT inside de list which block the script.

Here is the snippet html

<div class="prodUnitCat">
<ul class="fixFacetZindex clearBoth" id="divChildrenCategoryFilter_0">
/ul>
<ul data-lvlcat="2">
<li data-idcat="1000015739"><span>ACCESSOIRES LITERIE</span> (9)</li>
<li data-idcat="1000015760"><span>ELEMENT DE LIT</span> (5)</li>
<li data-idcat="1000015773"><span>LIT</span> (9)</li>
<li data-idcat="1000015794"><span>LIT D'APPOINT</span> (2)</li>
</ul>
</div>

and here my code

  prod = db.select(sql)
  for record in prod:


        cat1       = record[10]
        cat2       = record[11]
        cat3       = record[12]
        cat4       = record[13]

        # loading check
        WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id(headerFieldID))


        # cat selection
        driver.find_element_by_xpath(u"//span[contains(text(),'" + cat1 +"')]").click()
        driver.find_element_by_xpath(u"//span[contains(text(),'" + cat2 + "')]").click()
        #driver.find_element_by_css_selector("span[contains('" + cat2 +"')]").click()
        driver.find_element_by_xpath(u"//span[contains(text(),'" + cat3 + "')]").click()
        driver.find_element_by_xpath(u"//div[@id=\"categContent\"]/div[4]/ul/li/span[contains(text(),'" + cat4 + "')]").click()
        loginButtonElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
        loginButtonElement.click()
Andronaute
  • 379
  • 3
  • 12
  • 1
    What is the expected result? what are you trying to find? – styvane Jul 13 '15 at 08:44
  • Oh sorry i'm trying t find the LIT But i assume as there several occurence of "LIT" It does not work – Andronaute Jul 13 '15 at 10:12
  • Are you getting any errors with your current code? Thanks. – alecxe Jul 13 '15 at 12:52
  • Actually, the script block and finaly raise a error for the next step " cat3 " , because it does not find it. cat 3 doest not appear because it's dynamic when you click on cat 2 – Andronaute Jul 13 '15 at 13:04
  • @Andronaute then, it would make sense to use an explicit wait to wait for the cat3 to be present, right? I mean that it could be just that. – alecxe Jul 13 '15 at 13:13
  • It's already in explicit wait no? here is the error message NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"//span[contains(text(),'ENSEMBLE SOMMIER ET MATELAS')]"} – Andronaute Jul 13 '15 at 13:40
  • @Andronaute well, I don't see that. Right after you click on `cat2` wait for `cat3` to become present. – alecxe Jul 13 '15 at 14:03

2 Answers2

7

Use //span[text()='" + cat3 +"']. . Don't use contains keyword as it will try to search subString LIT. If you remove contains, then it will try to match only whole word 'LIT' with case sensitive.

Vishal Jagtap
  • 896
  • 11
  • 16
1

I don't have enough points to comment, but I am just adding to Vishal Jagtap's answer (which also worked for me - thank you!). There seems to be a typo in the 'code' bit of their response. It should be:

//span[text()='" + cat3 +"']

and not:

//span[text()='" + cat3 +"'].

Perhaps obvious, but it confused me (a relative beginner). One could also simplify the code a bit through making the string a variable/object:

search_string = "//span[text()='" + cat3 +"']"
driver.find_element_by_xpath(search_string).click()
arranjdavis
  • 657
  • 8
  • 16