I've a requirement to capture the payment ID from the text in an element using Selenium, python, pytest UI. The text is in the form, "Payment 3022594".
I was trying something like this.
PAYMENT_ID_TEXT_XPATH = "//div[@class='BottomLayoutHeaderWrapper']//h5[text()='Payment']"
paymentId = self.get_element(self.PAYMENT_ID_TEXT_XPATH, page)
if paymentId:
paymentId = paymentId.strip()
paymentIdValue = paymentId[8:]
This doesn't always work because sometimes paymentId is null resulting in TypeError,
TypeError: 'NoneType' object is not subscriptable
in the line where I am extracting paymentIdValue.
Is there better way to capture the payment ID from the text? The Payment ID will always be appended to the text "Payment" in the UI.
The only way I could see this working is with the changes below,
h5[text()='Payment']
will never work because it's expecting the full text in the H5 to be "Payment" and you've stated that it should be, "Payment 3022594". You need to change that to contains()
so it can be located.if paymentId:
will not work because if the element is not found, the previous line, self.get_element()
, will throw an exception.element.text
.Updated code
PAYMENT_ID_TEXT_XPATH = "//div[@class='BottomLayoutHeaderWrapper']//h5[contains(text(),'Payment')]"
paymentId = self.get_element(self.PAYMENT_ID_TEXT_XPATH, page).text.strip()[8:]