Python, Selenium. Is it possible to use '//span[text()=" if two characters in the phrase are variable?
Question:
I´m using Python Selenium and there´s an element in a combobox I want to select:
<span class="combobox__control"><input name="shippingPolicyId" value="Deutsche Post Brief Prio 2,55€ & 3,70€ (67 Angebote)" id="s0-0-0-24-7-23[15]-0-28-4-1-shippingPolicyId" type="text" role="combobox" aria-autocomplete="list" aria-roledescription="Use up and down arrow keys to navigate options. Options change based on text input" aria-haspopup="listbox" autocomplete="off" aria-owns="nid-sfp-1" aria-expanded="false" aria-controls="nid-sfp-1"><svg aria-hidden="true" class="icon icon--dropdown" focusable="false"><use xlink_href="#icon-dropdown"></use></svg></span>
The problem I have is, that the digits "67" in (67 Angebote) are variable, they change everytime I run the script.
here´s my snippet:
prioVersandOption = elem(
'//span[text()="Deutsche Post Brief Prio 2,55€ & 3,70€ (67 Angebote)"]/parent::div',
driver)
driver.execute_script("arguments[0].click();", prioVersandOption)
Is it possible to ignore these two digits? If it changes to "68" the code is not working anymore.
Cheers
Answers:
I’m not sure if this helps, buy you can use regex to ignore the 67:
import re
prioVersandOption = elem(
'//span[matches(text(), "Deutsche Post Brief Prio 2,55€ & 3,70€ \([0-9]+ Angebote\)")]',
driver)
driver.execute_script("arguments[0].click();", prioVersandOption)
If I have understood your question!
You can use contains() method instead of text() method. Text() allows you to find the exact match, while contains() as the name indicates checks if the specified text is present.
Code:
prioVersandOption = elem(
'//span[ contains (text(), "Deutsche Post Brief Prio 2,55€ & 3,70€" ) ]/parent::div',
driver)
driver.execute_script("arguments[0].click();", prioVersandOption)
contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on a partial text match.
For example, if we need to locate a button that has “Get started free” as its text, it can be located using the following line of code with Xpath.
Example: //*[ contains (text(), ‘Get started’ ) ]
Use this code
//Span[Contains(@value="Deutsche Post Brief Prio 2,55€ & 3,70€ ( Angebote)")]
In the place of 67 leave empty don’t enter anything
Try above code
I´m using Python Selenium and there´s an element in a combobox I want to select:
<span class="combobox__control"><input name="shippingPolicyId" value="Deutsche Post Brief Prio 2,55€ & 3,70€ (67 Angebote)" id="s0-0-0-24-7-23[15]-0-28-4-1-shippingPolicyId" type="text" role="combobox" aria-autocomplete="list" aria-roledescription="Use up and down arrow keys to navigate options. Options change based on text input" aria-haspopup="listbox" autocomplete="off" aria-owns="nid-sfp-1" aria-expanded="false" aria-controls="nid-sfp-1"><svg aria-hidden="true" class="icon icon--dropdown" focusable="false"><use xlink_href="#icon-dropdown"></use></svg></span>
The problem I have is, that the digits "67" in (67 Angebote) are variable, they change everytime I run the script.
here´s my snippet:
prioVersandOption = elem(
'//span[text()="Deutsche Post Brief Prio 2,55€ & 3,70€ (67 Angebote)"]/parent::div',
driver)
driver.execute_script("arguments[0].click();", prioVersandOption)
Is it possible to ignore these two digits? If it changes to "68" the code is not working anymore.
Cheers
I’m not sure if this helps, buy you can use regex to ignore the 67:
import re
prioVersandOption = elem(
'//span[matches(text(), "Deutsche Post Brief Prio 2,55€ & 3,70€ \([0-9]+ Angebote\)")]',
driver)
driver.execute_script("arguments[0].click();", prioVersandOption)
If I have understood your question!
You can use contains() method instead of text() method. Text() allows you to find the exact match, while contains() as the name indicates checks if the specified text is present.
Code:
prioVersandOption = elem(
'//span[ contains (text(), "Deutsche Post Brief Prio 2,55€ & 3,70€" ) ]/parent::div',
driver)
driver.execute_script("arguments[0].click();", prioVersandOption)
contains(): Similar to the text() method, contains() is another built-in method used to locate an element based on a partial text match.
For example, if we need to locate a button that has “Get started free” as its text, it can be located using the following line of code with Xpath.
Example: //*[ contains (text(), ‘Get started’ ) ]
Use this code
//Span[Contains(@value="Deutsche Post Brief Prio 2,55€ & 3,70€ ( Angebote)")]
In the place of 67 leave empty don’t enter anything
Try above code