Selenium in Python: Breaking a While Loop After an Onclick Attribute Changes - python

I'm looking for a way to stop a loop that continues to click a "Next Page" button until it is no longer clickable on a web-based system, which is only indicated by a change of the Onclick value from:
onclick="javascript:__doPostBack('m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20','')"
to
onclick="return false;__doPostBack('m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20','')"
My problem is that I currently don't have a way to get Selenium to recognize that this change has been made.
As of now, I've been able to write something like this:
while (driver.find_element_by_class_name('rgPageNext').is_enabled()) is True:
time.sleep(1)
elem = driver.find_element_by_class_name('rgPageNext').click()
However, the button is always enabled even though the button doesn't do anything after it reaches the last page, which results in Selenium continually trying to click the button.
The complete info from inspecting the element of the button is as follows:
<input type="button" name="m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20" value=" " onclick="javascript:__doPostBack('m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20','')" title="Next Page" class="rgPageNext">
<input type="button" name="m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20" value=" " onclick="return false;__doPostBack('m$main$RadGrid1$ctl00$ctl03$ctl02$ctl20','')" title="Next Page" class="rgPageNext">

You can try checking the 'onclick' attribute, whether its string startswith return false; , if so you should end the loop at the time. For that, change the while loop condition to -
while not driver.find_element_by_class_name('rgPageNext').get_attribute('onclick').startsw‌​ith("return false;"):

Related

Automate a login when login button has no "id" using python

portfolio123.com recently changed their whole website.
When a login submit button has no ID label:
<button type="submit" class="btn btn-primary" ng-disabled="data.loading">{{ data.loading ? 'Please wait...' : 'Submit' }}</button>
how would one do an automated login? (this is how you "normally" do it
signInButton = browser.find_element_by_id('signIn')\n",
"signInButton.click()\n
I am trying to do this:
https://www.hongkiat.com/blog/automate-create-login-bot-python-selenium/
Doing a get by class btn btn-primary could work... But there are 4 buttons on this page that use that class...
Did they purposely make this modification so bots cannot get in?
You could use xpath
browser.find_element_by_xpath('//button[text()="Submit"]').click()
You can test it identifies the element correctly by pasting the string in to a browser console between $x('') symbols
e.g.
>> $x('//button[text()="Submit"]')

Why doesn't Selenium find element by name? (Python, Firefox)

I have the following HTML
<button name="_eventId_confirmed" class="btn btn-green margin-bottom-20 bold medium" autofocus="">
and the following Python
btn = driver.find_element_by_name('_eventId_confirmed')
Running this code returns an error
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [name="_eventId_confirmed"]
Just before this non-working HTML/code-combination I have the following HTML element:
<input name="registration" id="registration" class="size-28" maxlength="7" autofocus="" autocomplete="off" type="text" data-original-title="" title="" style="">
which I successfully access with
elem = driver.find_element_by_name("registration")
Why does the second one work but not the first one?
Edit: the problem was that a new window was opened and I needed to switch window handler. So, false alarm. Thank you all.
A "tag name" is not the same as a "name". The former refers to the HTML element's tag name, while the later refers to the HTML element's "name" attribute.
For example, in your first HTML snippet,
<button name="_eventId_confirmed" class="btn btn-green margin-bottom-20 bold medium" autofocus="">
button is the tag name while _eventId_confirmed is the (attribute) name.
This could be because of the modal dialogue, as you mentioned in a comment. Try
driver.switchTo().frame("ModalFrameTitle");
or
driver.switchTo().activeElement()
You can do it by using window_handles and switch_to_window method.
Before clicking the button the window handle as
window_before = driver.window_handles[0]
elem = driver.find_element_by_name("registration")
after clicking the button the window handle of newly opened window as
window_after = driver.window_handles[1]
then execute the switch to window methow to move to newly opened window
driver.switch_to.window(window_after)
driver.find_element_by_name("_eventId_confirmed").click()
Hope this help.

Unable to click on button using the Selenium library in Python because element is hidden (Only able to do so using JavaScript)

<a class="btn btn-xs btn-success" href="/user-location-auth/location?id=4013"
title="View User Location Access" data-toggle="tooltip" data-method="post">
<span class="glyphicon glyphicon-map-marker"></span></a> <span
class="glyphicon glyphicon-map-marker"></span> <<pseudo:before>>
</<pseudo:before>>
Hi all, I have been tasked with a project that requires clicking on a button in a table (with 14 cells). The first 13 cells are all unhidden, but the 14th one (where the buttons are located), are hidden unfortunately. I have tried action chains, I have tried hovering over the element and then clicking on it. I seem to be at a loss here. This piece of code did work, however I want to avoid using javascript as I want to replicate a user experience.
z4 = driver.find_element_by_css_selector('a.btn-success > span.glyphicon-map-marker')
driver.execute_script('arguments[0].click();', z4)
Hi everyone and cruisepandey, I found a solution that works. First, I waited for the presence of the element to be located. Once that occurred, I grabbed the element and moved to it with ActionChains (I think I was misusing ActionChains earlier). Then I was able to click on the Bootstrap button.
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, """
//tr[#data-key='2312312321321']/td/a[contains(#class, 'btn-xs btn-
success')]/span[contains(#class, 'glyphicon')]""")))
y = driver.find_element(By.XPATH, """//tr[#data-
key='2312312321321']/td/a[contains(#class, 'btn-xs btn-
success')]/span[contains(#class, 'glyphicon')]""")
#Use action chains to click on the button
ActionChains(driver).move_to_element(y).click().perform()

Message - "unknown error: cannot focus element" - in python selenium driver

<div id="ccodeinput">
<input class="dropdownheader2" type="TEXT" name="CCODE" value="435435" size="14" maxlength="22">
</div>
This is the code for the Search field(see screenshot - upper right side). I am unable to pass values from a list to this search bar. But I am able to press the search button beside it.
How can I enter values in the search field?
This is what I have tried:
inputElement = chrome_driver.find_element_by_id('ccodeinput')
inputElement.send_keys(435435)
As per the HTML you have shared the <input> tag is a child of <div> tag, so to put text within the intended element you can use :
chrome_driver.find_element_by_xpath("//input[#class='dropdownheader2' and #name='CCODE']").send_keys("435435")

Web Scraping with Python: Iinput text and click a button

I was doing some web scraping with python (Linkedin site) and got stuck with the following 2 issues: 1) How do I input text on a search bar? 2) How to click a button? First, this is the search bar code:
<input aria-autocomplete="list" autocomplete="off" spellcheck="false"
placeholder="Búsqueda" autocorrect="off" autocapitalize="off" id="a11y-
ember6214" role="combobox" class="ember-text-field ember-view" aria-
expanded="false">
To input the text I was using the xpath (and it works) but it changes every time I login into the site:
search = driver.find_element_by_xpath('//*[#id="a11y-ember997"]')
search.send_keys('MedMake')
So could I use instead part of the input bar code above so that I can rerun my script multiple times?
My second point is 2) how to click a button. Again I was using the xpath but it changes after every login. My code was:
button = driver.find_element_by_xpath('//*[#id="nav-search-controls-wormhole"]/button')
button.click()
I inspected the button code and I would instead like to use data-vertical="PEOPLE" or any other of this unique fields (the tag button is not enough since there are many buttons on Linkedin site). By the way,how are all these inner fields called? I believe part of my problem arises from the lack of html code understanding.
<button data-vertical="PEOPLE" data-control-
name="vertical_nav_people_toggle" data-ember-action="" data-ember-
action-8620="8620" data-is-animating-click="true">
Gente
</button>
If id attribute values are dynamic you can use other attributes with static values:
search = driver.find_element_by_xpath('//input[#placeholder="Búsqueda"]')
search.send_keys('MedMake')
button = driver.find_element_by_xpath('//button[normalize-space()="Gente"]')
button.click()
First one use xpath
//input[contains(#class,'ember-text-field')]
Second one use the xpath
//button[#class='vertical_nav_people_toggle']

Categories

Resources