Python Selen, как получить родительский элемент - PullRequest
1 голос
/ 12 октября 2019

Я хочу найти в тексте "Дорога" и получить позицию родителя об этом. На картинке a busy cat

My code get to "Road" text. but can't get to parent of position element
 T4 = driver.find_element_by_css_selector('table#tableID4')
 T4.text

Output: 'Road\nRoad\n2\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad\nRoad'

html tag:

<table id="tableID4">
            <tr id="bigTr"><td id="bigRoadTd_0_0" class="">
                                    <p style="display: none"></p>
                                    <div style="" class="tie"></div>
                                    <div style="display: none" class=""></div>
                                    <div style="display: none" class=""></div>
                                    <div style="display: none" class=""></div>
                                    <div style="" class="banker">Road</div>
                                </td><td id="bigRoadTd_1_0" class="">
                                    <p style="display: none"></p>
                                    <div style="display: none" class=""></div>
                                    <div style="display: none" class=""></div>
                                    <div style="display: none" class=""></div>
                                    <div style="display: none" class=""></div>
                                    <div style="" class="player">Road</div>

1 Ответ

1 голос
/ 12 октября 2019
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

WebDriverWait(driver, 3).until(
                        EC.presence_of_element_located((By.ID , 'bigRoadTd_0_0')))

parent = driver.find_element(By.ID,'bigRoadTd_0_0')
all_children = parent.find_elements(By.TAG_NAME,"div")
for child in all_children:
    print(child.get_attribute('outerHTML'))

Выше будет найден родитель и проанализированы дочерние элементы для их externalhtml.

Также вы не будете использовать селектор css, чтобы найти этот конкретный дочерний элемент, это будет:

element_html = driver.find_element(By.CLASS_NAME,'banker').get_attribute('outerHTML')

Это атрибуты, доступные для По классу:

ID = "id"
XPATH = "xpath"
LINK_TEXT = "link text"
PARTIAL_LINK_TEXT = "partial link text"
NAME = "name"
TAG_NAME = "tag name"
CLASS_NAME = "class name"
CSS_SELECTOR = "css selector"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...