0

How to pass variable inside my xpath and use it? Or please advise if there is a better way to do this. Then I will do IF condition, in case my page will contents appropriate report name.

enter image description here

here is a pseudo code: 
unique_report_name = ("rep" + str(randint(100, 200)))
create report...
search created report...
result = driver.find_elements_by_xpath("//*[contains(text(),'Report Name :"unique_report_name"')]")  
  if result is displayed: 
    do something...

1 Answer 1

1

From the HTML you posted, I don't see any quotation marks in Report Name : rep101 so these quotation marks may not be necessary. Based on the HTML, here's how I would write the XPath:

driver.find_elements_by_xpath("//h1[contains(text(), 'Report Name : " + unique_report_name + "')]")

However, if rep101 is actually surrounded by qutoation marks, you will need to use escape characters in Python to do this. Here's how I would refactor your XPath, if there are quotation marks in the text:

result = driver.find_elements_by_xpath("//*[contains(text(),'Report Name :\"unique_report_name\"')]")  

The \ character will escape your " characters in the XPath, so your string will not be interrupted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.