1

I need to find elements on a page by looking for text(), so I use xlsx as a database with all the texts that will be searched.

It turns out that it is showing the error reported in the title of the publication, this is my code:

        search_num = str("'//a[contains(text()," + '"' + row[1] + '")' + "]'")
        print(search_num)
        xPathnum = self.chrome.find_element(By.XPATH, search_num)
        print(xPathnum.get_attribute("id"))

print(search_num) returns = '//a[contains(text(),"0027341-66.2323.0124")]'

Does anyone know where I'm going wrong, despite having similar posts on the forum, none of them solved my problem. Grateful for the attention

2
  • What is self.chrome? Are you sure it is not self.driver?
    – Ivan
    Commented Feb 17, 2023 at 14:02
  • self.chrome = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=self.options) @Ivan Commented Feb 17, 2023 at 14:04

2 Answers 2

1

Lot more quotes appear, Use python format() function to substitute the variable.

search_num ="//a[contains(text(),'{}')]".format(row[1]) 
0
1

Looks like you have extra quotes here str("'//a[contains(text()," + '"' + row[1] + '")' + "]'")

Try changing to f"//a[contains(text(),'{row[1]}')]"

2
  • It showed the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//a[contains(text(),'0027341-66.2323.0124)']' is not a valid XPath expression. Commented Feb 17, 2023 at 14:11
  • fixed the xpath
    – Ivan
    Commented Feb 17, 2023 at 14:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.