Copying Xpath directly to Selenium does not work

Clash Royale CLAN TAG#URR8PPP
Copying Xpath directly to Selenium does not work
I have been trying to rightclick and copypaste an Xpath directly from a website, in which I get this
//*[@id="WishList"]/div[4]/div/p[1]
Then I try doing the following:
a = driver.find_element_by_xpath(By.XPATH('//* [@id="WishList"]/div[4]/div/p[1]'))
However, I get the error "str" object is not callable. Is it not possible to directly copy-paste Xpaths?
1 Answer
1
Your code line is syntactically incorrect
Try one of below instead:
a = driver.find_element_by_xpath('//* [@id="WishList"]/div[4]/div/p[1]')
a = driver.find_element(By.XPATH, '//* [@id="WishList"]/div[4]/div/p[1]')
a = driver.find_element('xpath', '//* [@id="WishList"]/div[4]/div/p[1]')
Note that in Python By.XPATH is not a method (as in Java) but a simple string: By.XPATH == 'xpath'
By.XPATH
By.XPATH == 'xpath'
If you want to locate dynamic element, try
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.common.by import By
a = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//* [@id="WishList"]/div[4]/div/p[1]')))
You've copied from already rendered page (JavaScript executed), but your script is trying to find element before JavaScript executed. Another possible problem - element might be located inside an
iframe– Andersson
Aug 10 at 18:47
iframe
Oh, how would I go about finding the true xpath then? I've copypasted the html into a pastebin file here: pastebin.com/BWvY68zM . I want to get all the <p class="head" ones where there is text within them, I have no idea how to find their xpaths tho, any ideas?
– Soxty
Aug 10 at 18:53
Simply try
//p[@class="head"]– Andersson
Aug 10 at 18:55
//p[@class="head"]
I sadly already tried this, it prompts the exact same error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: "method":"xpath","selector":"//p[@class="head"]"
– Soxty
Aug 10 at 18:57
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
None of them works, it simply says "Unable to locate element", yet it's a direct copypaste from chrome with the inspection tool :(
– Soxty
Aug 10 at 18:45