1

I am running a code that navigates through folders and finally arrives at a destination and downloads a file by just clicking on it.

For example, to find a folder I am using:

find_element_by_link_text("Pricing and Catalogs")

The problem is that the folder sometimes isn't written exactly like that. For example, it has double spaces.

So my question is: is it possible to find an element by text that contains certain words? For example something like this (I know it's not correct, I just want you to understand me):

find_element_by_link_text(containing "Pricing" and "Catalogs")

I searched for the answer but couldn't find what I was looking for. If this is duplicated I apologize and ask to be pointed in the right direction.

Thanks in advance!

EDIT: Using find_element_by_partial_link_text won't do it since there are other folders with either Pricing or Catalogs in their names.

<td class="ms-tv-item ctl00_PlaceHolderLeftNavBar_ctl02_WebTreeView_2" style="white-space:nowrap;"><a class="ctl00_PlaceHolderLeftNavBar_ctl02_WebTreeView_0 ms-tv-item ctl00_PlaceHolderLeftNavBar_ctl02_WebTreeView_1" href="javascript:_spNavigateHierarchy(this,'','TAKES YOU TO SOME PAGE',false,'FolderNode', '')" title="Pricing  and  Catalogues" id="ctl00_PlaceHolderLeftNavBar_ctl02_WebTreeViewt223" style="border-style:none;font-size:1em;">Pricing  and  Catalogues</a></td>
clasico90
  • 57
  • 7
  • Share your HTML of this link. – Ratmir Asanov Mar 20 '18 at 16:34
  • I use Java. Does Python have something like `find_element_by_partial_link_text()`? – SiKing Mar 20 '18 at 17:35
  • @SiKing Using `find_element_by_partial_link_text` won't do it since there are other folders with either **Pricing** or **Catalogs** in their names. – clasico90 Mar 20 '18 at 19:41
  • When you tried `find_element_by_link_text("Pricing and Catalogs")`, what is the error that you get? The reason why I ask is that the documentation says: "locates elements by the exact text they display". Since multiple spaces are automatically collapsed in HTML and display as a single space, this should have worked. – SiKing Mar 20 '18 at 20:10
  • I get an `NoSuchElementException` error. You are right, it normally works but in some cases it doesn't. As you can see in the HTML I provided, that case has 2 spaces between words and is causing the problem. That's why I want an alternative. – clasico90 Mar 20 '18 at 20:27
  • 'Catalogs' <> 'Catalogues'. – Alexey Dolgopolov Mar 20 '18 at 20:29

3 Answers3

1

You could try XPath:

Example:

find_element_by_xpath("//a[contains(text(), 'Pricing') and contains(text(), 'Catalogues')]")

EDIT You've misspelled link text. Code has 'Catalogs'. But HTML has 'Catalogues'. Corrected xpath.

  • Hi, that did not work. It generated a `NoSuchElementException` – clasico90 Mar 20 '18 at 16:45
  • @clasico90 Try corrected answer. Others will work too. – Alexey Dolgopolov Mar 20 '18 at 20:32
  • Well, damn. Works like a charm. I have past the last 3 days looking into this and it's because of a spelling mistake. Thanks. Marked as correct. – clasico90 Mar 20 '18 at 20:35
  • @clasico90 If you didn't know about finding XPath in a browser this may help https://stackoverflow.com/questions/22571267/how-to-verify-an-xpath-expression-in-chrome-developers-tool-or-firefoxs-firebug/22571294. Just try your xpath or css selector expressions before pasting into code. – Alexey Dolgopolov Mar 20 '18 at 20:41
1

Also, replacing text with node sometimes works better:

Example of usage:

find_element_by_xpath("//a[contains(node(), 'Pricing') and contains(node(), 'Catalogs')]")
Ratmir Asanov
  • 5,433
  • 5
  • 20
  • 36
0

To click on the intendeed element containing the texts Pricing and Catalogs you can use either of the following lines of code :

  • LINK_TEXT :

    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.LINK_TEXT, "Pricing  and  Catalogues"))).click()
    
  • XPATH :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(.,'Pricing') and contains(.,'Catalogs')]"))).click()
    
  • CSS_SELECTOR :

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.ctl00_PlaceHolderLeftNavBar_ctl02_WebTreeView_0.ms-tv-item.ctl00_PlaceHolderLeftNavBar_ctl02_WebTreeView_1[title='Pricing  and  Catalogues']"))).click()
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217