Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过xpath使用find_元素查找元素_Python_Xpath_Webdriver - Fatal编程技术网

Python 通过xpath使用find_元素查找元素

Python 通过xpath使用find_元素查找元素,python,xpath,webdriver,Python,Xpath,Webdriver,我试图通过xpath查找元素,但它不返回任何内容。 似乎除了xpath之外,所有的find_元素方法都可以工作。 我试图找到可点击的文本《美国黄埔露营地》 def searchCondition(): elem = driver.find_elements_by_xpath("//div[@aria-label='PONDEROSA CAMPGROUND']") elem.click() 这是我试图查找元素的URL。 您必须将xpath指向交互元素,例如a,input,按

我试图通过xpath查找元素,但它不返回任何内容。 似乎除了xpath之外,所有的find_元素方法都可以工作。 我试图找到可点击的文本《美国黄埔露营地》

def searchCondition():
elem = driver.find_elements_by_xpath("//div[@aria-label='PONDEROSA CAMPGROUND']")
elem.click()
这是我试图查找元素的URL。


您必须将xpath指向交互元素,例如
a
input
按钮
并且必须使用预期条件诱导一些显式等待,以检查元素是否已全部设置为执行单击操作。比如说-

WebDriverWait(web, 20).until(EC.element_to_be_clickable((By.XPATH,"//a[@alt='PONDEROSA CAMPGROUND']")))

elem = web.find_element_by_xpath("//a[@alt='PONDEROSA CAMPGROUND']")

elem.click()
单击图像后,将打开新选项卡,如下所示。要查看此操作,请在单击操作后添加一些等待时间-


您正在单击
div
您需要单击锚定标签
//a[@alt='Pounderosa Camperground']
使用此XPath它不返回任何内容是什么意思?你说这是文本。您没有使用
.text
。你不能点击文本,当点击文本时,文本不会返回任何内容。请详细说明“不返回任何内容”的含义。您的XPath是正确的。还有别的问题。可能有多个元素,结果是一个数组?
find\u elements\u by\u xpath
返回一个
列表,即使它只找到一个元素
elem[0]。click()
works.@buddybobii,我的意思是这个过程结束了,没有给我任何东西。网络浏览器(Chrome)刚刚自动关闭。我正在尝试单击“谢谢您的帮助。我没有工作。我在问题中添加了完整的脚本。@DJTL您的代码中出现了什么异常?@DJTL可能是您使用的浏览器窗口的默认大小,请在
driver.get()之后添加
driver.maximize\u window()
。此外,如果元素显示():打印(元素)元素,我看不出此
的目的。单击()其他:打印(“无可用”)搜索条件()
您只需执行
元素。单击()
我将其更改为此。这是可行的,但我不想使用@id,因为每次尝试其他营地时它都会更改。elem=driver。通过xpath(“/*[@id='rec-card-233118_campground'])查找元素(如果元素显示():elem.click()focuschange()其他:ActionChains(driver)。发送键(key.F5).perform()搜索条件()
import time
from time import sleep
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
import pause
import pyperclip

# Definitions
ID = ""
PW = ""
SRCH = "Ponderosa Campground"
URL = "https://www.recreation.gov/search?q=Ponderosa%20Campground"
   
now = datetime.now()
options = Options()
options.headless = False

# executable_path for webdriver
driver = webdriver.Chrome(
    executable_path='C:/chromedriver.exe',
    options=options)

wait = WebDriverWait(driver, 50)
driver.get(URL)

def searchClick():

CampBtn = wait.until(EC.element_to_be_clickable((By.XPATH,"//a[@alt='PONDEROSA CAMPGROUND']"))).click()
elem = driver.find_element_by_xpath("//a[@alt='PONDEROSA CAMPGROUND']")

    if elem.isDisplayed():
        print(elem)
        elem.click()
    else:
        print("no availability")
        searchCondition()

    time.sleep(20)
WebDriverWait(web, 20).until(EC.element_to_be_clickable((By.XPATH,"//a[@alt='PONDEROSA CAMPGROUND']")))

elem = web.find_element_by_xpath("//a[@alt='PONDEROSA CAMPGROUND']")

elem.click()