md-option for selenium python, failed most of the time - python

Hi I have the following code
<md-option ng-repeat="sector in allSector | orderBy:'name'" ng-value="{"id":239,"name":"MU-BHND-JC32-0033","regionInventory":{"id":18,"regionName":"Mumbai","zone":{"id":4,"name":"WEST","longitude":73.1812,"country":{"id":3,"name":"INDIA","countryCode":"IND"},"latitude":22.3072},"stateCode":"MU","latitute":19.076,"longitude":72.8777},"latitute":19.29117616,"longitude":73.04375901,"coordinates":null,"centroid":null}" tabindex="0" class="ng-scope md-ink-ripple" role="option" aria-selected="false" id="select_option_982" aria-checked="true" value="[object Object]" style>
Basically I want to select the element MU-BHND-JC32-0033 from the above code and click on it.
As id keeps on changing I am not open for going for id.
if someone can help me out in clicking using xpath and ng-value.
I tried the following but it failed
subRegOpt = driver.find_element_by_xpath("//md-option[#ng-value='MU-BHND-JC32-0033']")
sleep(5)
subRegOpt.click()

Try to replace
subRegOpt = driver.find_element_by_xpath("//md-option[#ng-value='MU-BHND-JC32-0033']")
with
subRegOpt = driver.find_element_by_xpath("//md-option[contains(#ng-value, 'MU-BHND-JC32-0033')]")

Related

Unable to click on an ahref link using Python Selenium

This is the html code:
<div id="dcf-page-export-buttons" class="no-print" style="display: block;">
<a id="dcf-saveaspdf" href="#" target="_blank" class="k-button">
Save as PDF
</a>
<a id="dcf-saveaspng" href="#" target="_blank" class="k-button">
Save as Image
</a>
<a id="dcf-printPdf" class="k-button" href="#">
Print
</a>
<a id="dcf-btnClose" class="k-button" href="#">
Close
</a>
</div>
I want to click on the Print href but it isn't being clicked. Here is my code:
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
print = driver.find_element_by_id("dcf-printPdf")
print.click()
Before finding element by id for print, I had clicked on Export href which opened a new tab and after opening of the new tab, I'm trying to click on print but getting an error. This is the error:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
Let me know if I'm going wrong somewhere or if there is a problem in the html.
The first part of the question has been answered. This is the 2nd part:
On clicking on this Print button:
This window gets opened. It's not a new tab per se, but just a new window. Within that window, I want to click on the Save button. Is there a way to go about doing that? This is how the view looks like:
And here is the html code.
<cr-button class="action-button" aria-disabled="false" role="button" tabindex="0">
Save
</cr-button>
Here is my code till now:
exportLink = driver.find_element_by_link_text("Export")
exportLink.click()
driver.switch_to.window(driver.window_handles[1])
driver.execute_script("document.getElementById('dcf-user-info').style.display = 'none';")
time.sleep(1)
print = driver.find_element_by_link_text("Print")
print.click()
This is the snip of the error log. I've added the snip because I'm unsure of the error.
Small continuation of the error:
You need to switch to new tab first then you can click on Print using that Id that you have been trying.
Switch to new windows like this :
driver.switch_to.window(driver.window_handles[1])
As you can see from the error, the element you trying to access is not interactable.
So, the problem is not with HTML.
I can't see the page you working on, but the problem can be that element you trying to access is out of the view.
Try this
from selenium.webdriver.common.action_chains import ActionChains
print = driver.find_element_by_id("dcf-printPdf")
ActionChains(driver).move_to_element(print).click(button).perform()

Xpath Selectors - is it possible to find elements by title in selenium?

I've been trying to build a bot that scrapes the name of followers liking a picture in instagram. The website opens a popup box with the accounts in side, and the box seemingly refreshes the account links as you scroll it. I've written a code that will open the box and then scroll it, but i can't get selenium to scrape the account names. My code looks like this to scroll the pop up box:
realscroll_box = browser.find_element_by_xpath('/html/body/div[4]/div/div[3]/div')
while last_ht != ht:
last_ht = ht
time.sleep(2)
ht = browser.execute_script('''
arguments[0].scrollTo(0, arguments[0].scrollHeight);
return arguments[0].scrollHeight;
''', realscroll_box)
namelinkstemp1 = realscroll_box.find_elements_by_xpath('//*[contains(#href,"/")')
But it gives me this error:
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //[starrts-with(#href,"/") because of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[starrts-with(#href,"/")' is not a valid XPath expression.
The text i'm trying to extract looks like this:
<a title="instagramusername" href="/instagramusername/">
<div class=" Igw0E IwRSH eGOV_ ybXk5 _4EzTm ">
<div class="_7UhW9 xLCgt qyrsm KV-D4 fDxYl rWtOq">
<div class=" Igw0E rBNOH eGOV_ ybXk5 _4EzTm ">instagramusername</div>
</div>
</div>
</a>
Any help is appreciated, xpath queries are very new to me.
Thanks :)
Starts-with function finds the element based on attribute value. In your code function is not correct . Also //*[contains(#href,"/") , here you missed ]
Correct one : //a[contains(#href, '/')]
//*[starts-with(#href,'/')]
or
//a[starts-with(#href,'/instagramuse')]

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.

How to fix 'obscured' error in selenium?

On a non-public webpage I have the following html content:
<span click.delegate="placeCurrentInjection()" style="display:flex" class="au-target" au-target-id="611">
<ui-button glyph="glyph-iclamp" small="" primary="" disabled.bind="currentPlaceDisabled"
style="width: auto; max-width: 20em;" class="au-target ui-small ui-button primary" au-target-id="612" role="button" data-value="">
<span class="ui-indicator"><!--anchor--></span>
<ui-glyph glyph.bind="glyph" class="au-target ui-icon ui-btn-icon glyph-iclamp" au-target-id="36"><!--view-->
<svg>
<use tabindex="-1" x="0" y="0" class="au-target"
au-target-id="11" xlink:href="#glyph-iclamp"></use>
</svg>
<!--anchor--></ui-glyph><!--anchor-->
<!--anchor-->
<span class="ui-label">Place current injection<!--slot--></span>
<!--anchor--></ui-button> </span>
which shows a button like follows:
and I want to click on that button, using the following code in selenium 3.8.0:
elem = WebDriverWait(self.webdriver, time_sec).\
until(EC.element_to_be_clickable((By.XPATH, "//span[contains(text(), 'Place current injection')]")))
elem.click()
However, I get the following error:
E ElementClickInterceptedException: Message: Element <span class="ui-label"> is not clickable at point (782.2999877929688,156.03334045410156) because another element <ui-button class="au-target ui-small ui-button primary"> obscures it
Despite the fact the actual text is not obscured in any way and I can click on it - how to fix this issue?
The element you located is just the text on a label of the button. You need to click on the button itself, which is actually the element from the error message you received. The button is the parent element so you can use the text to locate it by moving to the parent
(By.XPATH, "//span[contains(text(), 'Place current injection')]/..")

Selenium Python, Find element Input box problems

I'm trying to automate some processes at work on our inventory site and I'm having trouble inputing text into a simple search box!
Here is the website code:
<td class="GJCH5BMASD" style="">
<input type="text" class="GJCH5BMD1C GJCH5BME1C" style="font-size: 13px; width: 100%;">
</td>
Here is my code:
opens = driver.find_element_by_css_selector(".GJCH5BMD1C GJCH5BME1C").click()
I keep receiving the error:
ElementNotVisibleException: Cannot click on element
Any thoughts?
Well nevermind!
I fixed it by changing:
opens = driver.find_element_by_css_selector(".GJCH5BMD1C GJCH5BME1C").click()
to:
opens = driver.find_element_by_css_selector(".GJCH5BMASD").click()
You need to provide . with every class name using css_selector, you can also try as below :-
opens = driver.find_element_by_css_selector(".GJCH5BMD1C.GJCH5BME1C").click()

Categories

Resources