0

When using the explicit text it works and the found element is clicked on but not when I use the variable:

This is the div: <div unselectable="on" class="x-grid3-cell-inner x-grid3-col-object_name">68915969-LS</div>

This works:

result = driver.find_element(By.XPATH,"//div[contains(text(),'68915969-LS')]")
action = ActionChains(driver)
action.move_to_element(result).click().perform()

When I replace '68915969-LS' by a variable nothing happens (no error, found line is not clicked on):

doc_number = '68915969-LS'
result = driver.find_element(By.XPATH,"//div[contains(text(),doc_number)]")
action = ActionChains(driver)
action.move_to_element(result).click().perform()

Any idea why?

Cheers Daniel

Daniel
  • 59
  • 4

3 Answers3

0

You're actually looking for "doc_number" as a string. You need to resolve your value and join it into your xpath string.

Try this:

driver.find_element(By.XPATH,"//div[contains(text(),'"+doc_number+"')]")
RichEdwards
  • 3,423
  • 2
  • 6
  • 22
0
driver.find_element(By.XPATH,f"//div[contains(text(),'{doc_number}')]")

If you want you can also f string and put your variable in.

Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32
0

Thanks a lot. You're absolutly right. Somehow I struggled that the xpath is just a string... I used this one and it worked fine :

result=driver.find_element(By.XPATH,"//div[contains(text(),'%s')]"% str(doc_number))
Daniel
  • 59
  • 4