Selenium No Such Element Exception - Python - javascript

I need to automate some stuff behind some hyperlinks from my university's SAP Portal. I figured using Selenium is the way to go here. But it turns out most of the web elements there are faked and created from JavaScript, that is probably why webdriver cannot SEE them.
Here is what I have to click:
<a class="urLnkDragRelate" id="Link6c5f851b" ct="LN" st="" tabindex="0" ti="0" title="Feedback Form" onkeydown="return (sapUrMapi_Link_activate('Link6c5f851b',event))" href="javascript:void(0)" target="" onclick="return htmlbDoEvent(this,'C','onclick','0','htmlb_222143_0',6,1,'',0);">
<span class="urFontStd">
<span ct="TV" class="urTxtStd">Feedback Form</span>
</span>
</a>
Here is the page source : https://pastebin.com/Dpc36nxL
Screenshot for understanding: https://imgur.com/a/qvxhVFA
I tried to use a workaround, by using ActionChains to open up developer console, and inject an artificial CLICK on the desired element, but even that failed because when I try to inject JavaScript at the end, the link here changes each time.
document.querySelector("#Link5908e99d")
Something like this:
assert "Feedback Form" in driver.page_source
# open up the developer console
driver.send_keys(keys.Keys.CTRL+keys.Keys.SHIFT+'i')
driver.perform()
time.sleep(3)
action.send_keys(keys.Keys.ENTER)
# inject the JavaScript...
action.send_keys("document.querySelector("#Link5908e99d").click()"+keys.Keys.ENTER)
action.perform()
How do I go forward with this?
The solution was to switch to the iframe which totally went unnoticed and then find the element using its relative XPath from the iframe.

I think link id is dynamic here and if you want to click on a element then please use below xpath.
locateClick= WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH,"//a[contains(text(),'Link')]")))
locateClick.click();

Related

Add popup based on class (most likely)

I'm trying to use Tampermonkey to add a popup on pages in the Canvas LMS. It's a forum, and after each post there is a "Reply" option, which is what I want to add the popup to. But when I click the "Reply" link, no popup appears. It opens the Reply box, as normal, but my popup is nowhere to be seen.
The code looks roughly like this:
<div class="entry-controls hide-if-collapsed hide-if-replying">
<div class="notification" data-bind="notification"></div>
<a role="button" class="discussion-reply-action entry-control" data-event="addReply" href="#">
<i class="icon-replied"></i>
<span aria-hidden="true">Reply</span>
<span class="screenreader-only">Reply to Comment</span>
</a>
</div>
The JS code I'm trying to add is:
document.querySelectorAll('.discussion-reply-action').forEach(item => {
item.addEventListener('click', event => {
alert("Popup text here");
})
})
In addition to .discussion-reply-action, I've tried using .entry-controls, .notification, .entry-control, even stuff like span[aria-hidden="true"]. Nothing seems to work.
I know the Tampermonkey script itself is applying correctly, because it has other functionality that is showing up as usual.
Any idea why this bit isn't working for me? I'm a complete JS noob, for what that's worth.
This got answered in the replies, but just wanted to formally note that it came down to delaying my code injection. I was trying to attach to elements that loaded after the doc. Once I got behind them, it worked fine.

Protractor automation button click inside '_ngcontent-c6'

My application has a button, with _ngcontent-c6 and i am unable to click on it.
element(by.css("button[class*='primary-btn']")).click()
The parent of button -span >class="ng-star-inserted" is being located.
Message:
Failed: No element found using locator: By(css selector, button[class*='primary-btn'])
<b>
<span _ngcontent-c6="" title="" class="ng-star-inserted">
<button _ngcontent-c6="" class="button primary-btn">
Create New Request
</button>
</span>
</b>
Try locator by xpath :
element(by.xpath("//*[#class='button primary-btn' and contains(text(),'Create New Request')]")).click()
element(by.buttonText("Create New Request"))
or
element(by.css(".button.primary-btn"))
If none of solutions provided above won't work (xpath by #Sameer and #Frian or my) make sure to wait until your button is interactive using Protractor Expected Conditions
You can try with below code snippet as well.
element(by.xpath("//span[#class='ng-star-inserted']//child::button[#class='button
primary-btn']").click();

Can't click button with selenium

I'm a newcomer when it comes to javascript and selenium. I have created a simple add to cart project, but the one i am currently working on im having some troubles. The HTML code is:
<div class="buttons-set" id="shipping-method-buttons-container">
<button type="button" class="dark" onclick="shippingMethod.save()" onkeypress="shippingMethod.save()">Continue</button>
<span id="shipping-method-please-wait" class="please-wait icon icon--notch" style="display:none;">
Loading next step... </span>
</div>
I can't seem to figure out anyway where i can click the Continue button. I have tried things such as
driver.findElement(By.linkText("Continue")).click();
driver.findElement(By.xpath(//*[#id='shipping-method-buttons-container']/button)).click();
and many other combinations but none seemed to work.
Try getting the element by its class name:
driver.find_element_by_class_name("dark").click();
I believe you have used implicit wait. If not we need to add it.
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Also try this below xpath.
driver.findElement(By.xpath("//button[contains(text(),'Continue']")).click();
Hope this helps. Thanks.
Try this below code using cssSelector locator.
driver.findElement(By.cssSelector("button[class='dark'][type='button']")).click();
OR
Try to click the button using java-script executor.
WebElement continue_button = driver.findElement(By.cssSelector("button[class='dark'][type='button']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", continue_button);

Fancy Box Not Working?

Unable to pin with fancy box is not working. I followed the directions on this link...
http://fancyapps.com/fancybox/
<h5 class="product-title">$25.00 Off First Lyft Ride</h5>
<p class="product-desciption">Get a free $25.00 towards your ride with Lyft when you use the activation code below! If you need a return ride make sure to use the Uber Promo Codes, and get a free ride back!<br><br><font size="2"><b><a class="fancybox" rel="group" href="https://www.youtube.com/watch?v=WyJsb4ANIy8"><font color="#858585">Watch Video Review<font></a></b></font></p>
All javascript and link rels are correct. A class looks correct as well. Any ideas?
Web page is www.couponcodeshero.com
You have to use youtube.com like :
<a class="various fancybox.iframe" href="http://www.youtube.com/embed/L9szn1QQfas?autoplay=1">Youtube (iframe)</a>
Also i have found error in your console for fancybox. Error is :
Uncaught TypeError: $(...).fancybox is not a function
So this means that you have not found fancybox.js file data on this page. OR your fancybox js jQuery is conflicting with other jQuery library.

Ghost.py - click specific button

I'm trying to click specific button but with no result yet.
Using Python 3.4.2 and Ghost.py.
<a class="button" title="" ref="1" id="details" href="javascript:void(0);">
</a>
This code is under many div's and li's.
The simplest answer is welcomed!
You can evaluate a line of Javascript with Ghost.py and use the click method on the appropriate DOM element you get with getElementById:
page, resources = ghost.evaluate("document.getElementById('details').click();", expect_loading=True)
UPDATE
To get the link by class use the following line
ghost.evaluate("document.getElementsByClassName('button')[0].click();", expect_loading=True)
There is another version you can use to select and click the first link with an ref="1" attribute on your page:
ghost.evaluate("document.querySelector('a[ref="1"]').click();", expect_loading=True)

Categories