Python Selenium Scrape with dropdown - python

I am using python and selenium to scrape a website. However I am having issues selecting an element from a dropdown list. When I run this script on windows in the python IDE I can get my script to work:
listElement = driver.find_element_by_id('header-transactionTypeOptions')
transElement = driver.find_element_by_id('container-primary-4-transactionTypeOptions')
listElement.click()
time.sleep(2)
transElement.click()
<input id="header-transactionTypeOptions" class="jpui input header focus-on-header wrap right text-float-left " type="button" aria-disabled="false" aria-expanded="false" aria-label=" SHOWING:: Activity since last statement" value="Activity since last statement">
However, when I run this on my ubuntu instance with pyvirtualdisplay it tells me it cannot locate the element. In my research for using selenium with a drop down - the suggested answers are using "Select". However there is no "Select ID" on the elements on this webpage.
I have include the elements below, as other options to reference in trying to find an answer:
<button id="iconButton-transactionTypeOptions" type="button" class="jpui input-icon icon text-overflow" aria-hidden="true" tabindex="-1"><span class="jpui expanddown icon input-icon hasError"></span></button>
<span class="primary" id="container-primary-1-transactionTypeOptions">Jul 21, 2016 statement </span>

Related

Selenium Unable to Locate Element, but it surely exists

EDIT FROM FUTURE, MAY BE YOUR SOLUTION: The page that I was trying to deal with opens on a new tab which I've missed to notice. My driver was trying to find the element on the very first tab, that is why it was unable to locate element.
--
--
--
I tried all solutions on Stackoverflow, however still got the same problem.
I am trying to select a specific option from a dropbox. It looks like this:
click to see the dropbox.
The HTML that belongs to this dropbox is as follows:
<td ct="GLC" lsdata="{1:'STANDARD'}" id="WD18" class="lsContainerCell lsGLCTopVAlign lsContainerCellVAlign--top urLayoutDefault--grid" valign="TOP" align="left" style="white-space:normal;"><span id="WD19-r" class="lsField lsField--standalone lsField--explicitwidth lsField--hasellipsis lsField--hashelp lsField--list lsField--focus lsField--interactiontarget" aria-live="assertive" style=""><input id="WD19" ct="CB" lsdata="{2:'WD1A',3:'10',4:'2021\x2f2022',27:'WD17'}" lsevents="{Select:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" type="text" autocomplete="off" tabindex="0" ti="0" class="lsField__input" readonly="" value="2021/2022" role="combobox" aria-haspopup="listbox" aria-readonly="false" aria-controls="WD1A" aria-expanded="false" aria-autocomplete="list" style="width:150px;" title=""><span id="WD19-btn" class="lsField__help" tabindex="-1" ti="-1" aria-hidden="true" role="button"></span></span></td>
Nothing I have tried did work because Selenium can not locate element. I've tried it with By.ID and By.XPATH. Last thing I have tried is this:
driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td/div/div[1]/span/span[1]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]")
But I still got the same error for the thousandth time:
Message: no such element: Unable to locate element
Thanks in advance.
Try this Xpath, avoid absolute path (click element, get xpath -_-)
driver.find_element(By.XPATH, "//td[#ct='GLC']")

Python & Selenium - Find element by label text

Im trying to locate and click an element (checkbox) from a big selection of checkboxes on a html site using python and selenium webdriver. HTML code looks like this:
HTML Code
<div class="checkbox-inline col-md-5 col-lg-3 col-sm-6 m-l-sm rightCheckBox">
<input type="checkbox" checked="checked" class="i-checks" name="PanelsContainer:tabsContentView:5:listTabs:rights-group-container:right-type-view:2:right-view:2:affected-right" disabled="disabled" id="id199"> <label>Delete group</label>
</div>
My problem is that the only unique identifier is:
<label>Delete group</label>
All other elements/id's/names are used by other checkboxes or changes from page to page.
I have tried the following code:
driver.find_element_by_xpath("//label[contains(text(), 'Delete group')]").click()
But I only get error when using this.
Error: element not interactable
Anyone able to help with this?
Try the below xpath
//label[contains(text(), 'Delete group')]//ancestor::div//input
Try with Javascript.
checkBox = driver.find_element_by_xpath("//label[text()='Delete group']//ancestor::div//input")
# Scroll to checkbox if its not in screen
driver.execute_script("arguments[0].scrollIntoView();", checkBox)
driver.execute_script("arguments[0].click();", checkBox)
Note : As per HTML shared by you, checkbox is in Disabled state, so i am not sure click will trigger any action. However above code will click your checkbox.

How to access the value of Angular's moment picker with Selenium and Python?

I have a web page with an Angular moment picker to show a time. Using Selenium and Python I want to assert that a specific time is set in the picker's input field.
I tried to check if the time occurs in the page source using assert '13:19' in driver.page_source but this statement fails because the time value is not included in the DOM.
How to access this value of the input field from a Selenium with Python script?
This is the HTML of the picker:
<input type="text" id="observation-timedate" ng-disabled="readOnly"
ng-class="{'time-picker-timeDate': !readOnly}" readonly="true"
class="input-block-level ng-valid ng-isolate-scope moment-picker-input
ng-valid-min-date ng-valid-max-date time-picker-timeDate ng-not-empty
ng-dirty ng-valid-parse ng-touched"
ng-model="action.observationTime" format="LT"
moment-picker="observationTimeDate"
tabindex="0"
aria-invalid="false"
style="">
I only managed to access the time value using JavaScript:
time = self.driver.execute_script("return document.getElementById(\"observation-timedate\").value;")

Python/Selenium - Getting the element after an element

I need to close a popup window from the website itself by pressing the "X" window on the top right. Here is the shortened, relevant part of my code:
chromedriver = r'C:\Users\do\Desktop\chromedriver.exe'
browser = webdriver.Chrome(chromedriver)
url = 'Fake.com'
browser.get(url)
browser.find_element_by_id('imgAttachmentsImg').click()
# I need to close out the window after this
The issue is that there are no unique identifiers for the "X" button itself. However, the pop up itself does have a unique identifier. I just need to be able to flow it down to the X button.
Info from the page:
1. V<div class="ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable ui-resizable" tabindex="-1" role="dialog" aria-describedby="attachmentsDialogOverview" aria-labelledby="ui-id-3" style="position: absolute; height: auto; width: auto; top: 239px; left: 102px; display: block;">
2. <div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
3. <span id="ui-id-3" class="ui-dialog-title">Attachments</span>
4. V<button type="button" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only ui-dialog-titlebar-close" role="button" aria-disabled="false" title="close">
5. <span class="ui-button-icon-primary ui-icon ui-icon-closethick"</span>
6. <span class="ui-button-text">close</span></button>
I am new to using Python and Selenium. I switched over from VBA and still don't exactly understand all the syntax yet, so I do make mistakes! I did find a bandaid solution by just having sendkeys press Escape. But I am trying to actually understand how to actually solve this. I am not sure what errors I made with my solution:
browser.find_element_by_xpath("//span[#id, 'ui-id-3']/following-sibling::button").click()
Questions
Why did my solution not work?
How would I locate "ui-id-3"(on line 3) and get to the element on Line 4 to click it?
How would I locate "ui-id-3"(on line 3) and get to the element on Line 5 to click it? (just so I know how to move across multiple elements)
Relevant links I looked over:
Following-Sibling Python Selenium
Find next sibling element in Python Selenium?
Using XPath Selector 'following-sibling::text()' in Selenium (Python)
Python + Selenium WebDriver - Get div value if sibling contains string
What is the correct syntax for using a variable and following-sibling in Python Selenium?
The xpath has a problem. this should work:
browser.find_element_by_xpath("//span[#id='ui-id-3']/following-sibling::button").click()
To get to the span below it, you could use:
browser.find_element_by_xpath("//span[#id='ui-id-3']/following-sibling::button/span")
by the way, if you're more comfortable with CSS, the equivalent paths would be:
div#ui-id-3 > button
div#ui-id-3 > button span:nth-of-type(1)

How to click a button from Python

I am using Anaconda2, Jupiter, and Chrome browser.
I am writing below code which is running successfully but now I want to click on button.
<a href="#" action="exportSelected" class="btn btn-default">
<i class="glyphicon glyphicon-download"></i> Export
</a>
What should I write in Python to access this?
find_element_by_partial_link_text('btn btn-default').click()
It's giving an error.
"btn btn-default" is not a link text, but class names.
You can use one of below solutions:
Locate by exact link text:
find_element_by_link_text('Export').click()
Locate by compound class name:
find_element_by_css_selector('a.btn.btn-default').click()
Locate by action attribute
find_element_by_css_selector('a[action="exportSelected"]').click()
Thank you all replying me and suggesting pyhthon doc. appreciate it.
So i have used very simple lines to get my job done.
driver.find_element_by_xpath('copy your xpath').
which is very simple
driver.find_element_by_xpath('//[#id="app_content"]/div[2]/div/div/section/div/div[2]/div[1]/div[1]/div/div[2]/a[1]').click()
driver.implicitly_wait(5)
driver.find_element_by_xpath('//[#id="export_button"]').click()

Categories