How to jump between two different elements located same level? - python

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

Related

How to get the element id by strong text using Selenium 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

How to click <a class href > link question using Selenium Python

Hi I am new to Selenium Python Webdriver. Trying to click the link Abc by using find_element_by_link_text which didn't work ,
<a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=0000&sid=-111111111">Abc</a>
I will appreciate the help. Any idea how to use xpath for this case?
<div id="favCol1" class="col-md-6 col-sm-6" style="padding: 2px; margin: 0px;">
<div id="favId2861214" class="elig-item">
<div title="Remove payer from favorites list" class="delete-favorite" data-itemname="Aetna" data-favid="2861214" data-dsid="28" data-lobid="1"></div>
<a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=1111&sid=-000000">Abc</a>
<span class="side-text"></span>
</div>
<div id="favId2869169" class="elig-item">
<div title="Remove payer from favorites list" class="delete-favorite" data-itemname="CIGNA" data-favid="2869169" data-dsid="317" data-lobid="1"></div>
<a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=0000&sid=-11111133323">efg</a>
<span class="side-text"></span>
</div>
<div id="favId2861157" class="elig-item">
<div title="Remove payer from favorites list" class="delete-favorite" data-itemname="Florida Blue" data-favid="2861157" data-dsid="35" data-lobid="1"></div>
<a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=2323&sid=-1111111">xyz</a>
<span class="side-text"></span>
</div>
<div id="favId2861963" class="elig-item">
<div title="Remove payer from favorites list" class="delete-favorite" data-itemname="Humana" data-favid="2861963" data-dsid="82" data-lobid="1"></div>
<a class="favorite-link " href="https://onesource.passporthealth.com/_members/query/osusf/Default.aspx?dlpvid=1233&sid=-000021212">HIJ</a>
<span class="side-text"></span>
</div>
</div>
Tried:
.find_element_by_xpath("//*[contains(#class, 'favorite-link') and contains(text(), 'Abc')]/a").click()
update:
The whole block was embedded in an iFrame. That's why I couldn't access. Had to switch to that frame.
driver.find_element_by_link_text("Abc").click()
Just click the tag with the link text Abc.
driver.find_element_by_xpath("//*[text()='Abc']").click()
To click on the element with text as Abc you can use either of the following Locator Strategies:
Using LINK_TEXT:
driver.find_element(By.LINK_TEXT, "Abc")
Using XPATH:
driver.find_element(By.XPATH, "//div[#class='elig-item']//a[#class='favorite-link ' and text()='Abc']").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:
Using LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Abc"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='elig-item']//a[#class='favorite-link ' and text()='Abc']"))).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 click an input role as radio located under in selenium using python

I am doing some automation script for the website https://silkdb.bioinfotoolkits.net in selenium using python language where I have you can see a button named "Blast" that pops up a window with some default values on click.
<div data-meta="Field" id="program" role="radiogroup" class="next-radio-group next-radio-button next-radio-button-medium">
<label dir="ltr" aria-checked="true" class="next-radio-wrapper checked "><span class="next-radio-single-input">
<input role="radio" tabindex="0" type="radio" aria-checked="true" class="next-radio-input"></span><span class="next-radio-label">blastn</span>
</label>
<label dir="ltr" aria-checked="false" class="next-radio-wrapper "><span class="next-radio-single-input">
<input role="radio" tabindex="-1" type="radio" aria-checked="false" class="next-radio-input"></span><span class="next-radio-label">blastx</span></label>
<label dir="ltr" aria-checked="false" class="next-radio-wrapper "><span class="next-radio-single-input"><input role="radio" tabindex="-1" type="radio" aria-checked="false" class="next-radio-input"></span><span class="next-radio-label">tblastn</span></label>
<label dir="ltr" aria-checked="false" class="next-radio-wrapper "><span class="next-radio-single-input"><input role="radio" tabindex="-1" type="radio" aria-checked="false" class="next-radio-input"></span><span class="next-radio-label">blastp</span></label></div>
The default label named "blastn" is selected on defualt. But I want to select lable named "blastp".
I have tried many things using xpath but I am unable to get it selected.
Please help me in this.
It is important to send the click events to the appropriate receiving elements which can actually react to the click event. In this case, i think the click must be send to the input element, not the span.
Please try with the following xpath.
//span[#class="next-radio-label" and contains(text(),'blastp')]/preceding-sibling::span/input
Please use following xpath to click your option //span[contains(text(),'blastp')]
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:
driver.get("https://silkdb.bioinfotoolkits.net/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#btn-blast"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#program label:last-child span:nth-child(2)"))).click()
Using XPATH:
driver.get("https://silkdb.bioinfotoolkits.net/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='btn-blast']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='next-radio-label' and text()='blastp']"))).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
Browser Snapshot:

Scrapy, Selenium, jQuery Dropdown inconsistent behavior with select tag having style="display: none;"

I'm trying to set dropdowns on this page:
The first dropdown and the fourth dropdown are very similar (brands and country). This is the code I'm using for getting the brand (oem) and country:
oem = Select(wd.find_element_by_css_selector("#alBrandsList"))
oem.select_by_visible_text("Acer")
countries = Select(wd.find_element_by_css_selector("#alCountriesList"))
countries.select_by_visible_text("Albania")
The dropdown is technically hidden, but it somehow seems to work for the device/oem dropdown. For the countries dropdown it is saying that the content isn't visible (which it is). Here's the HTML code it's pulling from:
<select class="pretty-dropdown" datatosent="brand" id="alBrandsList" name="alBrandsList" selectorid="alPhoneModelsList" target="/AdvanceLookup/GetPhoneModels/" style="display: none;">
...
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 232px;">
<span class="ui-icon ui-icon-triangle-2-n-s"></span>
<span>Please select brand(s)</span>
</button>
<select class="pretty-dropdown" datatosent="country" id="alCountriesList" name="alCountriesList" selectorid="alCarriersList" target="/AdvanceLookup/GetCarriers/" style="display: none;">
...
</select>
<button type="button" class="ui-multiselect ui-widget ui-state-default ui-corner-all" aria-haspopup="true" style="width: 232px;">
<span class="ui-icon ui-icon-triangle-2-n-s"></span>
<span>Please select country</span>
</button>
Any idea why it works on the first one but not the second one?
As both the <select> tags for the first and the fourth dropdown on the page https://willmyphonework.net/AdvanceLookup is having the property style="display: none; you can't use Select Class. Instead you need to to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver.get('https://willmyphonework.net/AdvanceLookup')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='ui-multiselect ui-widget ui-state-default ui-corner-all']//span[text()='Please select brand(s)']"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[#class='ui-multiselect-checkboxes ui-helper-reset']//li/label/input[#title='Acer']"))).click()
driver.find_element_by_xpath("//ul[#class='ui-helper-reset']//li/a/span[#class='ui-icon ui-icon-circle-close']").click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='ui-multiselect ui-widget ui-state-default ui-corner-all']//span[text()='Please select country']"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//ul[#class='ui-multiselect-checkboxes ui-helper-reset']//li/label/input[#title='Albania']"))).click()
driver.find_element_by_xpath("//div[#class='ui-multiselect-menu ui-widget ui-widget-content ui-corner-all']//following::ul[6]//a[#class='ui-multiselect-close']/span[#class='ui-icon ui-icon-circle-close']").click()
Browser Snapshot:

how to click button using find_element_by_xpath with inner html in selenium webdriver python3.7

first_click_content = driver.find_element_by_xpath("//div[#class='recent-report-wrapper']/div[2]/div/div[6]/div[2]")
print(first_click_content.get_attribute('innerHTML')
The above code give the result like this:
<button class="buttonWhite js-report-rerun">Re-run</button>
<button class="buttonWhite marginLeft js-report-edit">Edit</button>
<button class="buttonWhite marginLeft js-report-remove">Remove</button>
<button class="buttonWhite marginLeft js-report-save" style="display: none;">Save </button>
<button class="buttonWhite marginLeft js-report-view-errors" style="display: none;">View Errors</button>
<button class="buttonReportGreen marginLeft js-report-view" style="display: none;">View</button>
<button class="buttonReportGreen marginLeft js-report-download" style="display: inline-block;">Download</button>
I want to click the first button, how can I do that?
As the all the buttons are JavaScript enabled element you need to induce WebDriverwait for the desired element to be clickable and you can use either of the following solutions:
CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.buttonWhite.js-report-rerun"))).click()
XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='buttonWhite js-report-rerun' and contains(.,'Re-run')]"))).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