简体   繁体   English

如何使用 Selenium 找到隐藏的 class

[英]How to find hidden class with Selenium

I am currently working on a demo Selenium project with Python.我目前正在使用 Python 进行演示 Selenium 项目。 I have been able to navigate to a page but when trying to collect text within a "div class" selenium fails to find the HTML: Code to be collected我已经能够导航到一个页面,但是当尝试在“div 类”中收集文本时,selenium 无法找到 HTML:要收集的代码

I have made use of the wait functionality but the code still does not find the Html element.我已经使用了等待功能,但代码仍然找不到 Html 元素。

Any suggestions on how to resolve this issue would be appreciated, please see my code below: Image of my selenium任何有关如何解决此问题的建议将不胜感激,请参阅下面的代码:我的 selenium 的图像

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
import json


# establish Json dict
global data
data = {}

global date
date = '''&checkin=2021-02-22&checkout=2021-02-28&adults=1&source'''

def find_info(place):
    data[place] = []
    driver = webdriver.Chrome('chromedriver.exe')
    driver.get("https://www.airbnb.co.uk/")
    time.sleep(2)

    #first_page_search_bar
    search_bar = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "_1xq16jy")))
    time.sleep(2)
    search_bar.clear()
    time.sleep(2)
    search_bar.send_keys(f"{place}")
    time.sleep(2)
    enter_button = driver.find_element_by_class_name("_1mzhry13")
    enter_button.click()

    #load page
    WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "_ljad0a")))
    page = driver.current_url
    new_url = page.replace("&source", date)
    # driver = webdriver.Chrome('chromedriver.exe')
    driver.get(new_url)
    time.sleep(3)
    click_button = driver.find_element_by_xpath('//*[@id="menuItemButton-price_range"]/button')
    click_button.click()
    time.sleep(5)
    price = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '/html/body/div[16]/section/div/div/div[2]/div/section/div[2]/div/div/div[1]')))
    print(price)

find_info("London, United Kingdom")

I've fixed the xpath at the end of your script:我已经在脚本末尾修复了xpath

    price = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '(//div[@role="menu"]//div[@dir="ltr"])[1]/preceding-sibling::div')))
    print(price.text)

Explanation: Under the <div role="menu"... there are 3 <div dir="ltr"> elements and the first one happens to be just after the div you are looking for.说明:在<div role="menu"...下有 3 个<div dir="ltr">元素,第一个恰好在您要查找的 div 之后。 So we find that one and select the preceding sibling.所以我们发现一个和 select 是前面的兄弟。

Another recommendation: if you replace EC.presence_of_element_located to EC.element_to_be_clickable when you are looking for the input fields at the start you can get rid of a few time.sleep statements.另一个建议:如果您在开始时查找输入字段时将EC.element_to_be_clickable EC.presence_of_element_located则可以摆脱一些time.sleep语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM