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
Related
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
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
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 can I send a click to the following elements using Selenium?
Note: They are placed at the same page and both of them have the same class "btn btn-primary"
<button class="btn btn-primary" data-ng-click="ctrl.findInstrumentsBySearch(ctrl.filterInstrument);" data-ng-disabled="ctrl.disableButtonSearchInstrument();">
<span class="fa fa-search"></span> Pesquisar
</button>
<button class="btn btn-primary" data-ng-click="ctrl.downloadLimitInstrumentCsv(ctrl.filterInstrument,{ filename: "export.csv" });">
<span class="fa fa-file-excel-o"></span> Exportar
</button>
When I try to use the following I receive the error "IndexError: list index out of range":
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
browser = webdriver.Chrome()
browser.get(("https://line.bvmfnet.com.br/#/limits/1"))
python_button = browser.find_elements_by_xpath("//button[#class='btn btn-primary' and #data-ng-click='ctrl.findInstrumentsBySearch(ctrl.filterInstrument)']")[0]
python_button.click()
To click on the elements with text as save you can use either of the following Locator Strategies:
Pesquisar:
Using css_selector:
driver.find_element_by_css_selector("button.btn.btn-primary[data-ng-click*='findInstrumentsBySearch'][data-ng-disabled*='disableButtonSearchInstrument']").click()
Using xpath:
driver.find_element_by_xpath("//button[#class='btn btn-primary' and contains(#data-ng-click, 'findInstrumentsBySearch')][contains(., 'Pesquisar')]").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Exportar:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-primary[data-ng-click*='downloadLimitInstrumentCsv']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-primary' and contains(#data-ng-click, 'downloadLimitInstrumentCsv')][contains(., 'Exportar')]"))).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
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