How to get the element id by strong text using Selenium Python - python

HTML:
<div id="testModal" class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-shadow ui-hidden-container ui-dialog-exam ui-draggable" role="dialog" aria-labelledby="testModal_title" aria-hidden="false" aria-live="polite" style="width: auto; height: auto; left: 495px; top: 362px; z-index: 1002; display: block;"><div class="ui-dialog-titlebar ui-widget-header ui-helper-clearfix ui-corner-top ui-draggable-handle"><span id="testModal_title" class="ui-dialog-title"></span></div><div class="ui-dialog-content ui-widget-content" style="height: auto;">
<form id="testForm" name="testForm" method="post" action="/pages/juht/test/test.jsf" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="testForm" value="testForm">
<ul>
<li><a id="testForm:j_idt607:8:j_idt609" href="#" class="ui-commandlink ui-widget" onclick="PrimeFaces.addSubmitParam('testForm',{'testForm:j_idt607:8:j_idt609':'testForm:j_idt607:8:j_idt609'}).submit('testForm');return false;PrimeFaces.onPost();">
<strong>10.10.2022</strong>
19:00
<strong>Place »</strong></a>
</li>
</li>
<li><a id="testtestForm:j_idt607:10:j_idt609" href="#" class="ui-commandlink ui-widget" onclick="PrimeFaces.addSubmitParam('testForm',{'testForm:j_idt607:10:j_idt609':'testForm:j_idt607:10:j_idt609'}).submit('testForm');return false;PrimeFaces.onPost();">
<strong>11.10.2022</strong>
11:00
<strong>Place2 »</strong></a>
</ul>
<input type="hidden" name="javax.faces.ViewState" value="-4629203658604947866:263715935893442864" autocomplete="off"></form></div></div>
How can I print out the element id by just using strong text here?

You want the parent element's ID of that <strong> element? Then try:
element = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//strong[text()='10.10.2022']::a")))
element_id = element.get_attribute('id')
You will need to also import the following:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

To print the value of the id attribute as the elements are dynamic you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:
To print testForm:j_idt607:8:j_idt609:
Using XPATH and the text 19:00:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., '19:00')]"))).get_attribute("id"))
Using XPATH and the text 10.10.2022:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[.//strong[contains(., '10.10.2022')]]"))).get_attribute("id"))
Using XPATH and the text Place »:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[.//strong[contains(., 'Place »')]]"))).get_attribute("id"))
To print testtestForm:j_idt607:10:j_idt609:
Using XPATH and the text 11:00:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[contains(., '11:00')]"))).get_attribute("id"))
Using XPATH and the text 11.10.2022:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[.//strong[contains(., '11.10.2022')]]"))).get_attribute("id"))
Using XPATH and the text Place2 »:
print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[.//strong[contains(., 'Place »')]]"))).get_attribute("id"))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Related

Error in Clicking on SVG using Selenium Python

Following is HTML code. I want to click on Export to CSV.
<pre>
<div id="leo-title-bar" style="width: 100%">
<div class="container-fluid p-0"><div class="no-gutters" style="min-height: 100vh;">
<div class="col-12 d-flex flex-column">
<nav class="navbar navbar-dark bg-primary justify-content-start" style="height: 64px; flex-wrap: unset;">
<span class="navbar-brand" style="flex: 1 1 0%; font-size: 22px;">Agency Summary</span>
<svg aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8" data-prefix="fas" data-icon="download" class="svg-inline--fa fa-download fa-w-16 svg-shadow svg-icon-basic svg-icon-basic-hover" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<title id="svg-inline--fa-title-5AhAR2Z9sKF8">Export to CSV</title>
<path fill="currentColor" d="M216 0h80c13....."></path></svg>
</div></main>
</div>
</div>
</div>
</div>
</pre>
I have tried following code:
from selenium import webdriver
driver = webdriver.Edge(PATH)
driver.find_element_by_xpath('//div[#class="col-12 d-flex flex-column"]/*[name()="svg"][#aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8"]').click()
Getting an error:
selenium.common.exceptions.NoSuchElementException
Possibly you are missing a delay.
So adding some dummy sleep like
from selenium import webdriver
import time
driver = webdriver.Edge(PATH)
time.sleep(5)
driver.find_element_by_xpath('//div[#class="col-12 d-flex flex-column"]/*[name()="svg"][#aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8"]').click()
Should resolve your problem.
Also your locator looks bad. You have to create more reliable locator.
Also you should use Expected Conditions explicit waits, as following:
from selenium import webdriver
import time
rom selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Edge(PATH)
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, '//div[#class="col-12 d-flex flex-column"]/*[name()="svg"][#aria-labelledby="svg-inline--fa-title-5AhAR2Z9sKF8"]'))).click()
Change
.../*[name()="svg"]...
to
.../*[local-name()="svg"]...
in your XPath-expression, because your <svg...> element is in the namespace xmlns="http://www.w3.org/2000/svg". Thing is that name() matches namespace:name, but local-name() only matches the name without the namespace(-prefix).
The desired element is a svg element. To click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#leo-title-bar svg[data-icon='download']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='leo-title-bar']//*[name()='svg' and #data-icon='download']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
References
You can find a couple of relevant discussions on interacting with SVG element in:
How to access to 'rect' type element through Selenium-Python
Clicking on svg using selenium python

How to insert some text within the input using Selenium and Python

In a webpage I have the following element:
<div id="learning_form" class="learning_form">
<table cellspacing="0" cellpadding="0">
<tbody><tr><td><input type="text" id="answer" autocapitalize="off" autocorrect="off" autocomplete="off" placeholder="Odpowiedź" style="width: 360px;"></td></tr>
</tbody></table>
<div id="check"><h4 style="text-align: center;">Sprawdź</h4></div>
<div id="special_characters"><div> </div><div> </div></div>
</div>
How I can insert text here using selenium in python?
I tried using:
driver.find_element(By.ID,"learning_form").send_keys("some text")
but it doesn't work.
Snapshot of the element:
To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.learning_form#learning_form input#answer[placeholder='Odpowiedź']"))).send_keys("OłJeż")
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='learning_form' and #id='learning_form']//input[#id='answer' and #placeholder='Odpowiedź']"))).send_keys("OłJeż")
Note: You have to add the following imports :
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, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#answer"))).send_keys("value")
It seems you need to send values to the input with an id answer.
<input type="text" id="answer" autocapitalize="off" autocorrect="off" autocomplete="off" placeholder="Odpowiedź" style="width: 360px;">
Import:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to jump between two different elements located same level?

The problem is the following, in the same html I have a footer and a div located on the same level first I want to click on the img of the following code:
<footer>
<div layout="row" layout-align="center center" flex="" class="layout-align-center-center layout-row flex">
<img src="assets/images/vtr-icon-tecnico.png" class="icon" ng-click="vm.openModal($event, 'appt')" role="button" tabindex="0">
</div>
</footer>
and then click on an input of a input of the following code:
<div class="md-dialog-container ng-scope" tabindex="-1" style="top: 0px; height: 937px;">
<form name="appsForm" class="ng-pristine ng-invalid ng-invalid-required" style="">
<!-- ngIf: !links -->
<md-content class="md-padding ng-scope layout-column" layout="column" ng-if="!links" style="">
<md-input-container class="md-input-invalid"><label for="input_10557">Clave</label>
<input required="" type="password" name="password" md-maxlength="30" ng-model="key.password" class="ng-pristine md-input ng-empty ng-invalid ng-invalid-required ng-touched" id="input_10557" aria-invalid="true" ng-trim="false" style="">
</md-input-container>
</md-content>
</form>
</div>
Using xpath I tried with:
pass = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(#class,'ng-scope layout-column flex')]//div[#id='input_10555']")))
But the answer is the following in the console:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(#class,'ng-scope layout-column flex')]//div[#id='input_10555']"}
I use Python, Selenium Web Driver and Chrome Browser
thank you who has better understanding on how to do it
The desired element is an Angular element so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ng-pristine.md-input.ng-empty.ng-invalid.ng-invalid-required.ng-touched[id^='input_'][name='password']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class='ng-pristine md-input ng-empty ng-invalid ng-invalid-required ng-touched' and starts-with(#id,'input_')][#name='password']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to locate the textbox using selenium-webdriver in python

here's the code of the textbox :
<form>
<input autocomplete="off" id="number" name="number" type="tel" aria-describedby="error-for-number tooltip-for-number" data-current-field="number" data-fillr-id="424069765" data-fillr="bound" style="padding: 8px 10px; font-size: 14px; font-family: -apple-system, BlinkMacSystemFont, "San Francisco", "Segoe UI", Roboto, "Helvetica Neue", sans-serif;">
</form>
By the way it did't work when I tried to access it by id or name like this :
browser.find_element_by_id('number')
browser.find_element_by_name('number')
To locate the TextBox you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#number[name='number'][type='tel']")))
Using XPATH:
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='number' and #name='number'][#type='tel']")))
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

How to select the particular web element from the HTML code below using python selenium

Below is the HTML code and how to select element wither by using xpath or css selector
<button class="btn btn-sm ng-pristine ng-untouched ng-valid ng-empty" ng-repeat="ext in _view.getNonEmptyChildren()" ng-click="_navigate(ext.$id, _route.objectId, { 'navigator' : _route.navigator,
'relatedItemParentId': relatedItemParentId(ext.$parent)})" ng-class="{active: _view.getSelectedChild().$id == ext.$id}" ng-model="radioModel" btn-radio="'{ext[nameField]}'" aria-invalid="false">Clusters</button>
The desired element is an Angular element so you have to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-sm.ng-pristine.ng-untouched.ng-valid.ng-empty[ng-model='radioModel'][ng-class*='getSelectedChild']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-sm ng-pristine ng-untouched ng-valid ng-empty' and #ng-model='radioModel'][contains(.,'Clusters')]"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Categories

Resources