-1

I m trying to pass values of string to find_element_by_Xpath() to get the search and match for the part of value of attribute. How do I get this working?

I have tried it on Python3.7 and selenium library, Can't find a way to pass value from loop. I was trying with the following but doesn't work

datasets = [  'ds_pos_retail_outlet','ds_pos_retail_fullset', 'ds_pos_retail_ProfitCenter','ds_pos_retail_Section']

for x in datasets :

    refreshNow= driver.find_element_by_xpath('//button[starts-with(@aria-describedby, x) and @title="Refresh now"]')
    refreshNow.click()
    time.sleep(8)

I want to select and click a node with property value as passed with regex match

Tayyab
  • 11
  • 5
  • Possible duplicate of [How do I put a variable inside a string?](https://stackoverflow.com/questions/2960772/how-do-i-put-a-variable-inside-a-string) – JeffC May 13 '19 at 21:23
  • What regex are you referring to? You aren't using any regex. – JeffC May 13 '19 at 21:24

2 Answers2

0

One way is to use format():

xpath = '//button[starts-with(@aria-describedby, {}) and @title="Refresh now"]'.format(x)
refreshNow= driver.find_element_by_xpath(xpath)
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I think there is some problem here. I can't suppose to make it work. It is clicking the button with aria-describedby = " ds_pos_retail_fullsetdatasetMenu4". I was hoping this would work but unfortunately not for my case I think. Even though the dataset[1] element is "ds_pos_retail_outlet" – Tayyab May 14 '19 at 10:33
  • @Tayyab List indexes start at 0, so I think you mean `dataset[0]`. From here, you will need to verify that your xpath is correct to select the element you want. – Code-Apprentice May 14 '19 at 15:39
  • Okay, I get it working. Actually the issue is resolved by adding quotes at this part of you code like this " { } " – Tayyab May 14 '19 at 19:33
  • @Tayyab That makes sense. So for example `ds_pos_retail_outlet` needs to have quotes around it. That is part of the xpath syntax. – Code-Apprentice May 14 '19 at 20:13
0

Other than the format function, you can use simple string concatenation.

refreshNow = driver.find_element_by_xpath('//button[starts-with(@aria-describedby, ' + x + ') and @title="Refresh now"]')
nullptr
  • 3,701
  • 2
  • 16
  • 40