Can anyone give an example for the below scenario. Website link: http://www.uitestingplayground.com/dynamictable Scenario For Chrome process get value of CPU load.
CodePudding user response:
To get the value of Chrome process CPU load, You could use
//*[@]
where the xpath expression will select only the CPU load text node value only.
Example:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
table=driver.get('http://www.uitestingplayground.com/dynamictable')
driver.maximize_window()
CPU_load = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, '//*[@]'))).text
print(CPU_load)
Output:
Chrome CPU: 2.8%
P/S: As the value is dynamic,so it will be difference than that of mine.
CodePudding user response:
In case you are looking for the value inside the table you will need to get the number of header containing the CPU
text since the columns order is changing in that table, as in the code below.
In case the aim element is the element below the table //p[@]
XPath locator could be used.
Full Selenium code printing there texts could be as following:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("--start-maximized")
s = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=s)
url = 'http://www.uitestingplayground.com/dynamictable'
wait = WebDriverWait(driver, 10)
driver.get(url)
headers_locator = "//div[@role='row']//span[@role='columnheader']"
table_locator = "//div[@role='row' and(contains(.,'Chrome'))]//span[{0}]"
outer_locator = "//p[@class='bg-warning']"
cpu_column = 0
headers = wait.until(EC.visibility_of_all_elements_located((By.XPATH, headers_locator)))
for inx, header in enumerate(headers):
if "CPU" in header.text:
cpu_column = inx 1
break
table_locator = table_locator.format(cpu_column)
table_text = wait.until(EC.visibility_of_element_located((By.XPATH, table_locator))).text
outer_text = wait.until(EC.visibility_of_element_located((By.XPATH, outer_locator))).text
print("Table element text is " table_text)
print("Outer element text is " outer_text)
The output is changing from run to run, but it looks like:
Table element text is 7.1%
Outer element text is Chrome CPU: 7.1%
Process finished with exit code 0