how is it possible for me to let Selenium search for a specific Line in the Code of a Website?
I am searching for the Line i attached in the photo.
Thanks in advance!
CodePudding user response:
You can get the inner text of an element by either:
inner_text = element.get_attribute('innerText')
;inner_text = element.text
;
Therefore, you can scan all those divs with the condition of inner_text == "Fehler"
.
Since the ids follow a pattern, here's how you can scan the divs and select the element:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
def id_value(index):
return f"cdk-describe-message-{index}"
def find_innertext(driver, url, text, N):
driver.get(url)
for idx in range(N):
element = driver.find_elements(By.ID, id_value(idx))
if element.text == text:
return element
def main():
driver = webdriver.Firefox()
url = "https://...."
target = "Fehler"
number_of_divs = 40
return find_innertext(driver, url, target, number_of_divs)
if __name__ == "__main__":
element = main()
Notes:
- you have to know the number of divs elements there are in advance;
- consider using waits.
CodePudding user response:
This is what i got so far:
def main():
chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument("--incognito")
chrome_options.add_argument('ignore-certificate-errors')
#chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)
smalogin = "https://172.16.63.100/webui/login"
driver.get(smalogin)
driver.implicitly_wait(100)
email = driver.find_element(By.NAME, "username")
email.send_keys('user')
password = driver.find_element(By.NAME, "password")
password.send_keys('pass')
submit = driver.find_element("xpath",'//*[@id="login"]/button')
submit.click()
monitoring = driver.find_element(By.ID,"ennexos-element-monitoring")
monitoring.click()
statusliste = driver.find_element("xpath",'//*[@id="cdk-accordion-child-0"]/div/div/sma-feature-board-slot[1]/sma-navigation-link')
statusliste.click()
time.sleep(10)
if __name__ == '__main__':
main()