Home > Enterprise >  get value from element using selenium
get value from element using selenium

Time:03-01

Need to get the value from one element using several others as filters using selenium on a dynamic website(logtrail using kibana)

got this:

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import os

path2driver_ffox = os.path.join(os.path.abspath(os.getcwd()),"geckodriver")
path2driver_chr = os.path.join(os.path.abspath(os.getcwd()),"chromedriver")

try:
    driver = webdriver.Chrome(executable_path=path2driver_chr)
except:
    driver = webdriver.Firefox(executable_path=path2driver_ffox)


driver.get("https://log-viewer.mob.dev/app/logtrail#/?q="lw-00005"&h=web-sockets&t=Now&i=filebeat-*&_g=()")
print(driver.title)


driver.maximize_window()

using the example bellow i need to get the value from the last action where time = 28-2-2022 and lbl-00005 in li

can someone help?

<li id="IavYP38BMeu2l4fa6DvW" ng-repeat="event in events" on-last-repeat="" infinite-scroll="">
      <time>2022-02-28 10:20:49,864</time>
      <span ><a href="" ng-click="onHostSelected(event.hostname)">ws-web-sockets-pp</a></span>
      <span ><a ng-click="onProgramClick(event.program)">/web/serv/logs/ws/ws.log:</a></span>
      <span  ng-style="event.color? {color: event.color} : ''" ng-bind-html="event.message | ansiToHtml" compile-template="">2022-02-28 10:20:49,279 ws-web-sockets-pp-1 INFO  [null:-1] (executor-thread-14) - stat : <span >lbl-00005</span>:icifYWZuBe89EUYnMe-J3vIGOWQpG45-66vaB86d, MessageId: 894912413, request message: {"action":"act_VALUE","messageId":"894912413","type":"CALL","uniqueId":"894912413","payload":"{}"}</span>
    </li>

this works but:

time = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li[ng-repeat='event in events']>time"))).text

How do i know if this is the last (newest) record? how do i get the act_VALUE?

this

message= WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li[ng-repeat='event in events'] .message"))).text

doent seem to input the message that bellongs to the time that we get above

Cant copy this, can only send img :( enter image description here

I need to be able to search this page like this

Get the latest heartbeat (most recent record) and from that heartbeat get the messageid. this print is with filter only to show one record, normally the are thousands of li elements

need to able to put in var like this:

req=" {"action":"Heartbeat","messageId":"33","type":"CALL","uniqueId":"33","payload":"{}"}"

type="heartbeat"

msgid="33"

CodePudding user response:

# Locate the <li> element by it's ID,
# assuming that it has a unique ID ("IavYP38BMeu2l4fa6DvW")
li_element = driver.find_element_by_id("IavYP38BMeu2l4fa6DvW")

# Locate all <time> elements inside the <li> element
# There may be multiple <time> element found, we will store the result in a array
time_elements = li_element.find_elements_by_tag_name("time")

# Assume that the <time> element that contain the time is the first result
time_element = time_elements[0]

# Access the innerHTML of the <time> element
# The result should be 2022-02-28 10:20:49,864
time_string = time_element.get_attribute('innerHTML')

Edit: If you don't know the ID of the <li> element, you can try to locate it with XPath, example:

li_xpath = "Input xpath here"
li_element = driver.find_element_by_xpath(xpath)

CodePudding user response:

To handle the dynamic element use WebDriverWait() and wait for visibility_of_element_located() and following css selector.

time = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li[ng-repeat='event in events']>time"))).text

print(time)

chart= WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "li[ng-repeat='event in events'] .message .highlight"))).text

print(chart)

You need to import following libraries.

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
  • Related