我一直试图直接从网站上右键单击并复制粘贴Xpath,我在其中获得了
//*[@id="WishList"]/div[4]/div/p[1]
然后我尝试执行以下操作:
a = driver.find_element_by_xpath(By.XPATH('//* [@id="WishList"]/div[4]/div/p[1]'))
但是,我收到错误“ str”对象不可调用。是否无法直接复制粘贴Xpath?
答案 0 :(得分:1)
您的代码行在语法上不正确
尝试以下方法之一:
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]')
请注意,在Python中,By.XPATH
不是方法(如Java),而是一个简单的字符串:By.XPATH == 'xpath'
如果要查找动态元素,请尝试
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]')))