NoSuchElementException: Unable to select dropdown item - python-3.x

I'm unable to select the dropdown.
The code for the dropdown is
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr class="navigatorTable">
<td nowrap="" align="left">
<span class="dropdownbutton showSingle bound">
<a><img class="vAlignSub" src="/images/add.gif"> Create Account <img src="../../images/actionitems_collapse.gif"></a>
</span>
</td>
</tr>
</tbody>
</table>
And in python I have the following code.
select = Select(driver.find_element_by_xpath("//span[#class='dropdownbutton show fork']")).click()
select.select_by_value('1')
But I'm getting
selenium.common.exceptions.NoSuchElementException: Message: no such element:
Unable to locate element: {"method":"xpath",
"selector":"//span[#class='dropdownbutton showSingle bound']"}
Please help me with the code. Thanks

You can't use the Select in this case as though the class name says it's a drop down but it's span not an select html node. So, you can't use Select approach here.
You have to make sure the script waits until the span element is loaded.
#Imports required
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as E
Now get the element and click on it (check the xpath)
ele = WebDriverWait(driver,30).until(EC.presence_of_element_located((By.XPATH,"//span[#class='dropdownbutton showSingle bound']//img")))
ele.click()

Related

How to hover and click on hidden elements with Selenium Basic using VBA?

I followed a couple of solutions to this problem on this platform, especially Trouble selecting a hidden menu item using SeleniumBasic for vba
I was able to hover through the menu of the website I'm trying to automate, but I'm unable to click on the table row (Claims Submission) which is the second option when the table/form is visible.
My initial goal was to hover the visible menu of the page to show a drop down and I achieved that with a loop.
Now when the (claims) Menu is visible I need to click on the second option (Claims Submission) of that table. As directed from Trouble selecting a hidden menu item using SeleniumBasic for vba I attempted to use an xpath to reference the element.
It returned
Run-time error '7':
NoSuchElementError
Element not found for XPath=//a[.//span[contains(.,'Claims Submission')]]
Private WB As Selenium.WebDriver
Sub Test()
Set WB = New Selenium.ChromeDriver
WB.Start
WB.Get "https://health.axamansard.com/axamansardProviderlogin/Index.aspx"
Dim Menu As WebElement
Dim Menus As WebElements
Dim MenuSubmission As WebElement
WB.FindElementByName("txtUname").SendKeys "Almadina"
WB.FindElementByName("txtPass").SendKeys "Nhisdesk#1234"
WB.FindElementByName("btnSubmit").Click
Set Menus = WB.FindElementsByClass("a")
For Each Menu In Menus
If Menu.Text = "Claims" Then
WB.Mouse.MoveTo Menu
Set MenuSubmission = WB.FindElementByXPath("//a[.//span[contains(.,'Claims Submission')]]")
MenuSubmission.Click
End If
Next
End sub
WebScript
<table class="menu" id="Claims" width="150px" bgcolor="white" style="visibility: hidden;">
<tbody><tr height="19px">
<td class="menu"><a class="a"
href="../Forms/ClaimsBatch.aspx">Claims Batch</a></td>
</tr>
<tr height="19px">
<td class="menu"><a class="a"
href="../Forms/ClaimSubmission.aspx">Claims Submission</a></td>
</tr>
<tr height="19px">
<td class="menu"><a class="a"
href="../Forms/ClaimSubmissionList.aspx">Claims Submission List</a></td>
</tr>
<tr height="19px">
<td class="menu"><a class="a"
href="../Forms/ClaimsUpload.aspx">Claims Upload</a></td>
</tr>
<tr height="19px">
<td class="menu"><a class="a"
href="../Forms/Calimreviewlist.aspx">Claims Status</a></td>
</tr>
<tr height="19px">
<td class="menu"><a class="a"
href="../Forms/ProviderClaimsBatchDetails.aspx">Provider Claims Batch Details/Claim Credit Note Details</a></td>
</tr>
</tbody></table>
The xpath to the link Claims Submission seems a bit off. You can use either of the following Locator Strategies:
FindElementByXPath:
WB.FindElementByXPath("//table[#id='Claims']//td/a[.='Claims Submission']").Click
FindElementByXPath relative to Claims:
WB.FindElementByXPath("//a[.='Claims']//following::table[1]//td/a[.='Claims Submission']").Click

Find different element inside rows using selenium python

I am trying to get the values of available license inside the table using selenium in python3. I am able to get the values using XPATH, and iterate through each rows. But XPATH is not ideal, since the table might change and include an additional column, thus will fail to get the correct value.
The values I want, is 98,50,etc...
<div class="slick-cell l6 r6 licensesUsedValueGrid">
<div class="slick-cell odd" style="padding: 0px;width:100%;height: 44px;">
<div style="padding: 15% 4px 0px 0%;float: right;">98</div>
</div>
</div>
<div class="slick-cell l6 r6 licensesUsedValueGrid">
<div class="slick-cell odd" style="padding: 0px;width:100%;height: 44px;">
<div style="padding: 15% 4px 0px 0%;float: right;">50</div>
</div>
</div>
This is using XPATH, and it worked:
for i in range(1, rows, 2):
pak_id = browser.find_element_by_xpath(f'//*[#id="scrollBarDiv"]/div/div[{i}]/div[3]/div/div/div[1]').text
used_license = browser.find_element_by_xpath(f'//*[#id="scrollBarDiv"]/div/div[{i}]/div[7]/div/div').text
available_license = browser.find_element_by_xpath(f'//*[#id="scrollBarDiv"]/div/div[{i}]/div[8]/div/div').text
I would like to use class name or some other means so that even if they add another column to the table, i will be able to capture the right value of 'license used' and 'license available'
Picture of how it looks like on chrome:
Probably just:
browser.find_elements_by_css_selector('.licensesUsedValueGrid')
To extract all the values of available licenses using Selenium and Python you have to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.licensesUsedValueGrid>div.slick-cell.odd>div")))])
Using XPATH:
print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(#class, 'licensesUsedValueGrid')]/div[#class='slick-cell odd']/div")))])
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

Selenium selecting link within table - with all the same name

Cant click on the link due to the fact, all of the value I keep trying to select has the same name as well within the tables.
Tried xpath,by link, by text, by css none of it works. My current code:
driver.find_element_by_xpath('//[#id="homeapp"]/div/div/div[3]/div[1]/table/tbody/tr[3]/td[2]/div[1]/div[2]/span[1]/a').click()
Here is the relevant html code:
<tr class="bui-table__row"><td class="peg-table__cell--no-label bui-table__cell" data-heading="ID" scope="row"><a href="../../extranet_ng/manage/index.html?ses=8594ddf0718dec240a0c9e7991e108a8&name_id=11111" class="bui-link bui-link--secondary" target="_blank" data-track-ga="Groups: Home,Active Properties,Property ID">
1111(number i can click aswell)
</a></td> <td class="peg-table__cell--no-label bui-table__cell" data-heading="Name" scope="row"><div class="bui-avatar-block"><div class="peg-_-avatar-shrink bui-avatar"><img src="https://q-xx.bstatic.com/xdata/images/extra/square60/189090859.jpg?k=cc455d4d570f0e0c86a1b8329b16d53fdba96d78677cc87151d6181e45d38ec5&o=" alt="text i want to click" class="bui-avatar__image"></div> <div class="bui-avatar-block__text"><span class="bui-avatar-block__title"><a href="../../extranet_ng/manage/index.html?ses=8594ddf0718dec240a0c9e7991e108a8&name_id=11111" class="peg-property-link bui-link bui-link--secondary" target="_blank" data-track-ga="Groups: Home,Active Properties,Property Name">
Text i want to click
<!----> <!----></a></span> <span class="bui-avatar-block__subtitle"><!----> </span></div></div> <div class="bui-spacer--smaller"></div> <div class="peg-score-bar__inline bui-score-bar"><div class="bui-score-bar__item"><div class="bui-score-bar__header"><h2 class="bui-score-bar__title"></h2> <span class="bui-score-bar__score">93%</span></div> <div class="bui-score-bar__bar"><span data-value="9.3" class="bui-score-bar__value" style="width: 93%;"></span></div></div></div></td> <td class="peg-table__cell--no-label bui-table__cell" data-heading="Location"><div class="peg-address-wrapper"><span class="peg-flag-wrapper"><div class="peg-flag bui-flag"><img src="https://q.bstatic.com/backend_static/common/flags/16/nl/314ce6500532e846e25d6e3a7c824ef17c968446.png" class="bui-flag__flag" style="height: auto;"> <!----></div></span>
TEXT2
</div></td> <td class="peg-table__cell--no-label bui-table__cell" data-heading="Status"><span>Open/Bookable</span></td> <td class="bui-table__cell bui-table__cell--center" data-heading="Arrivals/departures & tomorrow"><span class="peg-counter--arrivals"><a href="../../extranet_ng/manage/search_reservations.html?ses=8594ddf0718dec240a0c9e7991e108a8&name_id=11111" data-track-ga="Groups: Home,Active Properties,Arrivals" target="_blank" class="peg-counter peg-counter--has"><span aria-label="80 unread" class="bui-bubble">
VALUE1
</span></a></span> <span class="peg-counter--departures"><a href="../../extranet_ng/manage/search_reservations.html?ses=8594ddf0718dec240a0c9e7991e108a8&name_id=11111&type=departure" data-track-ga="Groups: Home,Active Properties,Departures" target="_blank" class="peg-counter peg-counter--has"><span aria-label="80 unread" class="bui-bubble">
VALUE2
</span></a></span></td> <td class="bui-table__cell bui-table__cell--center" data-heading="Guest Messages"><a href="../../extranet_ng/manage/messaging_inbox.html?ses=8594ddf0718dec240a0c9e7991e108a8&name_id=11111" data-track-ga="Groups: Home,Active Properties,Guest Messages" target="_blank" class="peg-counter peg-counter--has"><span aria-label="102 unread" class="bui-bubble">
VALUE3
</span></a></td> <td class="bui-table__cell bui-table__cell--center" data-heading="extranet.com Messages"><a href="../../extranet_ng/manage/inbox.html?ses=8594ddf0718dec240a0c9e7991e108a8&name_id=11111" data-track-ga="Groups: Home,Active Properties,extrabnet.com Messages" target="_blank" class="peg-counter"><span aria-label="0 unread" class="bui-bubble">
0
</span></a></td></tr>
If you want to click the link which contains avatar in its class attribute use the following XPath expression:
//div[contains(#class,'avatar')]/descendant::a[contains(text(), 'text i want to click')]
where:
contains() is XPath Function allowing partial match on the attributes values and/or text
descendant - XPath Axis which matches children of the current node and their respective children
last part limits the search to hyperlinks which contain text you're looking for
The desired element is a ReactJS enabled element, so you have to induce WebDriverWait for the element to be clickable() and you can use either of the following solutions:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "text i want to click"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.peg-property-link.bui-link.bui-link--secondary[href*='/extranet_ng/manage/index'][data-track-ga*='Active Properties']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='peg-property-link bui-link bui-link--secondary' and contains(#href, '/extranet_ng/manage/index')][starts-with(#data-track-ga, 'Groups')]"))).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

Unable to locate element error while trying to locate an input box element using Selenium through Python

I´m trying to find a text box with Python and Selenium.. I Tried by_css_selector, bY XPATH, by ID, by name but the message is always the same:
Unable to locate element: #x-auto-225-input
this is the piece of html. I Want to find the textbox to fill it.
<td class="x-table-layout-cell" role="presentation" style="padding: 2px;">
<div role="presentation" class=" x-form-field-wrap x-component" id="x-auto-225" style="width: 150px;"></div>
<input type="text" class=" x-form-field x-form-text " id="x-auto-225-input" name="PURCHASE_ORDER_CODE_NAME" tabindex="0" style="width: 150px;">
</td>
My last attempt was:
pc = browser.find_element_by_css_selector("#x-auto-225-input").click()
pc.send_keys("7555425-1")
Looking at the html, id mentioned can be dynamic, so you can't put the static id in your identifier.
However, as name attribute is present in the html, you can use that to identify your element, like:
browser.find_element_by_name("PURCHASE_ORDER_CODE_NAME").click()
Updated answer as per discussion with the OP
As an iframe is present on the UI, you need to first switch to the iframe and then click on the element.
To switch to iframe you can use:
browser.switch_to.frame(browser.find_element_by_tag_name('iframe'))
and then use:
pc = browser.find_element_by_name("PURCHASE_ORDER_CODE_NAME")
pc.click()
pc.send_keys("7555425-1")
if you want to switch back to the default content, you can use:
browser.switch_to.default_content()
The desired element is a dynamic element so to invoke 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.x-form-field.x-form-text[id$='-input'][name='PURCHASE_ORDER_CODE_NAME']"))).click();
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#class=' x-form-field x-form-text ' and contains(#id,'-input')][#name='PURCHASE_ORDER_CODE_NAME']"))).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
Maybe you can try another "selector" approach. Ex(Javascript):
selenium.By.xpath('//*[#data-icon="edit"]')
driver.findElement(by).click()

find_elements_by_xpath() not producing the desired output python selenium scraping

I'm trying to find a tr by its class of .tableOne. Here is my code:
browser = webdriver.Chrome(executable_path=path, options=options)
cells = browser.find_elements_by_xpath('//*[#class="tableone"]')
But the output of the cells variable is [], an empty array.
Here is the html of the page:
<tbody class="tableUpper">
<tr class="tableone">
<td><a class="studentName" href="//www.abc.com"> student one</a></td>
<td> <span class="id_one"></span> <span class="long">Place</span> <span class="short">Place</span></td>
<td class="hide-s">
<span class="state"></span> <span class="studentState">student_state</span>
</td>
</tr>
<tr class="tableone">..</tr>
<tr class="tableone">..</tr>
<tr class="tableone">..</tr>
<tr class="tableone">..</tr>
</tbody>
Please try this:
import re
cells = browser.find_elements_by_xpath("//*[contains(local-name(), 'tr') and contains(#class, 'tableone')]")
for (e in cells):
insides = e.find_elements_by_xpath("./td")
for (i in insides):
result = re.search('\">(.*)</', i.get_attribute("outerHTML"))
print result.group(1)
What this does is gets all the tr elements that have class tableone, then iterates through each element and lists all the tds. Then iterates through the outerHTML of each td and strips each string to get the text value.
It's quite unrefined and will return empty strings, I think. You might need to put some more work into the final product.

Resources