Skip to main content
deleted 21 characters in body
Source Link
alecxe
  • 11.4k
  • 11
  • 51
  • 107

The problem looks (from the error) like it may have something to do with the extra '(' in your selector.

given Given you state the original statement works:

elem2 = driver.find_element_by_xpath("(//a[contains(text(),'Comment')])[3]")

and this is the bad one:

                  The given selector "(//a[contains(text(),\'Comment\')])[3]"

you can clearly see the difference when lined up. You appear to have an extra '(' at the start.

I believe

xp_str1 = str("\"//a[contains(text(),'Comment')])[")
xp_str2 = str(3)
xp_str3 = str("]\"")
str_elem = xp_str1 + xp_str2 + xp_str3

may fix that. I just tried it quickly in an online interpreter, and it appears to fix it.

The problem looks (from the error) like it may have something to do with the extra '(' in your selector.

given you state the original statement works:

elem2 = driver.find_element_by_xpath("(//a[contains(text(),'Comment')])[3]")

and this is the bad one:

                  The given selector "(//a[contains(text(),\'Comment\')])[3]"

you can clearly see the difference when lined up. You appear to have an extra '(' at the start.

I believe

xp_str1 = str("\"//a[contains(text(),'Comment')])[")
xp_str2 = str(3)
xp_str3 = str("]\"")
str_elem = xp_str1 + xp_str2 + xp_str3

may fix that. I just tried it quickly in an online interpreter, and it appears to fix it.

The problem looks (from the error) like it may have something to do with the extra '(' in your selector. Given you state the original statement works:

elem2 = driver.find_element_by_xpath("(//a[contains(text(),'Comment')])[3]")

and this is the bad one:

The given selector "(//a[contains(text(),\'Comment\')])[3]"

you can clearly see the difference when lined up. You appear to have an extra '(' at the start.

I believe

xp_str1 = str("\"//a[contains(text(),'Comment')])[")
xp_str2 = str(3)
xp_str3 = str("]\"")
str_elem = xp_str1 + xp_str2 + xp_str3

may fix that. I just tried it quickly in an online interpreter, and it appears to fix it.

Source Link
Mark Mayo
  • 1.2k
  • 12
  • 40

The problem looks (from the error) like it may have something to do with the extra '(' in your selector.

given you state the original statement works:

elem2 = driver.find_element_by_xpath("(//a[contains(text(),'Comment')])[3]")

and this is the bad one:

                  The given selector "(//a[contains(text(),\'Comment\')])[3]"

you can clearly see the difference when lined up. You appear to have an extra '(' at the start.

I believe

xp_str1 = str("\"//a[contains(text(),'Comment')])[")
xp_str2 = str(3)
xp_str3 = str("]\"")
str_elem = xp_str1 + xp_str2 + xp_str3

may fix that. I just tried it quickly in an online interpreter, and it appears to fix it.