Webpage link is suddenly unclickable using Selenium -WebDriver - java

I'm suddenly having an issue with clicking a link. This was working previously but now is not and I can't figure out what's wrong.
This is how the link is defined:
<td id="whiteseparator" class="generalcontent" width="22%">
<a title="NOT Received" onclick="javascript:gotosubmit('SalaryDetailsList'); return false;" href="#">NOT Received</a>
</td>
This is my code clicking the link. There are several of these and what I'm trying to do is get all of them so I can then iterate through all of them.
List<WebElement> mylinks = driver.findElements(By.cssSelector("a[title='NOT Received']"));
for (int i=0 ; i < mylinks.size() - 1; i++)
{
mylinks.get(i).click();
....(and so on)
It's not getting past the List statement. It just sits there. Any ideas?

This question is rather unclear, but what I've figured out, the button is not clickable, and you want Selenium to click on it.
This is an intentional design, as Selenium is designed as a Web Automation tool, that means if a user can't click it, Selenium can't either.
You might want to check this out: https://stackoverflow.com/a/21387564/4947486

Related

Element not found when executing findElement in Selenium

I'm trying to fill in multiple forms that come after each other, all the forms get filled swiftly with no errors because I make sure to add
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
before doing anything on a new page, and I know I'm on the correct page.
On the last form, I encounter this error :
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //*[#id="formtovalidate"]/fieldset[1]/div/label/input For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
So I went to check on the browser by taking a screenshot and the browser is on the correct page with the correct form, I also checked the xpath values and even tried other attributes.. nothing seemed to work.
So I went ahead and printed out the PageSource which showed a totally different page (not the previous page), I also noticed the this page flashed for a second before the final form appeared.
I also tried driver.navigate().refresh() but that didn't work. I kept searching and looking but nothing appeared. I also changed browsers, that did nothing..
This is the method I'm trying to execute:
private void method() {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"formtovalidate\"]/fieldset[1]/div/label/input")));
driver.findElement(By.xpath("//*[#id=\"formtovalidate\"]/fieldset[1]/div/label/input")).sendKeys(email); }
Update
Here's the form screenshot:
Here's the execution results:
Code:
String body_text = driver.findElement(By.tagName("body")).getText();
System.out.println(body_text);
Result: The form but in text
Code:
String body_innerHTML = driver.findElement(By.tagName("body")).getAttribute("innerHTML");
System.out.println(body_innerHTML);
Result: A different page :(
<zendesk-ticketing-form base-url="https://www.runescape.com/a=870/c=K0aO9WO69EI" css-cachebust="129" sitekey="6Lcsv3oUAAAAAGFhlKrkRb029OHio098bbeyi_Hv" grecaptcha="" has-valid-session="true" weblogin-url="https://secure.runescape.com/m=weblogin/a=870/c=K0aO9WO69EI/loginform?mod=www&ssl=1&dest=zendesk/support-form?form=360000065898">
<div class="x-display-none ie-error-display" data-js-ie-error="">
<section class="c-article">
<div class="c-article__content">
<h1>Error: Unsupported Browser</h1>
<p>
We do not support your web browser. Please use a supported web browser by choosing one below.
<br>
FireFox
<br>
Chrome
</p>
</div>
</section>
</div>
Code:
String pagesource = driver.getPageSource();
System.out.println(pagesource);
Result: Same as the previous one.. different page..
Firefox Page Source: https://pastebin.com/Kv15V2SK
Firefox Inspect Element of the page screenshot: http://prntscr.com/qvi6hc
This is weird, as the page source is different to the form!
I couldn't find time to solve your problem. If you want to do it on your own, please Search this on Google, "Shadow Root, Selenium", I had this kind of error before. What I know is, you cannot directly reach an element that stays inside of a shadow root, This is why you are not getting the source code inside of it.
What you need to do is go through the element step by step:
You have to expand the shadow root,
Here is shadow root expand function:
public static WebElement expand_shadow_element(WebElement element)
{
WebElement shadow_root = (WebElement)((JavascriptExecutor)driver).executeScript("return arguments[0].shadowRoot", element);
return shadow_root;
}
You can imagine this function like
.switchTo.frame()
for now..
After some researches you will understand the shadow root.
I hope I got the problem right..
Try this function, If you cannot, I will help you later on. Good Luck.
The PageSource from the <body> tag, containing...
<zendesk-ticketing-form base-url="https://www.runescape.com/a=870/c=K0aO9WO69EI" css-cachebust="129" sitekey="6Lcsv3oUAAAAAGFhlKrkRb029OHio098bbeyi_Hv" grecaptcha="" has-valid-session="true" weblogin-url="https://secure.runescape.com/m=weblogin/a=870/c=K0aO9WO69EI/loginform?mod=www&ssl=1&dest=zendesk/support-form?form=360000065898">
<div class="x-display-none ie-error-display" data-js-ie-error="">
<section class="c-article">
<div class="c-article__content">
<h1>Error: Unsupported Browser</h1>
<p>
We do not support your web browser. Please use a supported web browser by choosing one below.
<br>
FireFox
<br>
Chrome
</p>
</div>
</section>
</div>
...implies that the WebDriver driven Browsing Context was detected as a BOT and the navigation was blocked due to presence of reCAPTCHA.
There are different approaches to solve captcha / recaptcha. You can find a couple of relevant discussion in:
How to bypass Google captcha with Selenium
Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection
Update
From your comments now it is clear that you want to fill up the fields within the form:
At this point it is worth to mention that you had been redirected to this page for either of the following reasons:
You EmailID / UserID is banned / blocked from accessing the site.
You EmailID / UserID is black-listed from accessing the site.
As you have used a BOT to access/scrape the site which may have violated the T&C.
Solution
It would be tough to propose a solution to automatically fillup the fields as presumably the elements in the BAN APPEAL REQUEST page may be protected by Invisible reCAPTCHA and you may have to Programmatically invoke the challenge
As others have suggested, it appears RuneScape's website has detected that you're using a bot to interact with their site. It doesn't matter that you solved the captcha manually, as they can still detect automated behavior quite easily without one (and no, the navigator.webdriver flag is not their only way to detect this).
The captcha is meant to prevent automated interaction with their site, which means they don't want you using Selenium/WebDriver to interact with it. You should respect this, especially as it seems you want your account unbanned (going by the pasted snippets and screenshots), so trying to do exactly what they don't want won't win you any favors.

can't find my element (selenium beginner)

new programmer here-
I am trying to make a cookieclicker bot using selenium. Cookie clicker is a website game linked below
http://orteil.dashnet.org/cookieclicker/
I am attempting to locate the element, like so
WebElement element = driver.findElement(By.id("upgrades"));
and then I call it here:
upgrades = element.findElements(By.className("enabled"));
I want to make a list of "upgrades" that are "enabled". However, I get a null pointer exception on the second piece of code- but on the website there clearly is an element called "upgrades".
Thank you for your time!
edit:
this is the block of code from the game that I am trying to click
<div id="upgrades" class="storeSection upgradeBox"><div onclick="Game.UpgradesById[0].click(event);" class="crate upgrade enabled" onmouseout="Game.setOnCrate(0);Game.tooltip.shouldHide=1;" onmouseover="if (!Game.mouseDown)
and I am trying to fish out these "enabled" upgrades
<div onclick="Game.UpgradesById[0].click(event);" class="crate upgrade enabled" onmouseout="Game.setOnCrate(0);Game.tooltip.shouldHide=1;" onmouseover="if (!Game.mouseDown) {Game.setOnCrate(this);Game.tooltip.dynamic=1;Game.tooltip.draw(this,function(){return Game.crate(Game.UpgradesById[0],'store',undefined,undefined,1)();},'store');Game.tooltip.wobble();}" id="upgrade0" style="background-position:0px 0px;"></div>
I just checked on the website, your code requires modification.
Use the below code:
WebElement element = driver.findElement(By.id("upgrades"));
Actions acc=new Actions(driver);
acc.moveToElement(element).build().perform();
List<WebElement> upgrades = element.findElements(By.className("enabled"));
Let me know if this works for you.

Selenium Webdriver with Java - Click on an hyperlinked Dropdown Container

I need to click on an element inside a Dropdown container. I've tried several searchs but I haven't been able to find the correct solution. The select method doesn't work, and I still don't know how to work with Selectors when there's no ID, Name or Class related to it. Here's the HTML code:
Account<span class="caret"></span>
<div class="account-dropdown__container">
<ul>
<li>Account</li>
<li>Invite Friends</li>
<li>Zola Store Credit</li>
<li>Registry Settings</li>
<li>Orders You've Placed</li>
<li><a>Log out</a></li>
</ul>
</div>
The first piece of code is a button, but if I put my mouse over it, it will show the Dropdown container that I am talking about. If I put my mouse over it without clicking, it will show the list of the Dropdown Container. (And I would also like to know how to hover an element to show the list without clicking it, because its hidden).
My question is, then: how can I click on Registry Settings?
It doesn't have an ID, nor a class (although it is inside the class account-dropdown__container). I think I can use By.name("Registry Settings"), but since is not visible unless the Dropdown list is open, it won't click and it will show Css Selector not found error. Care to help? Thanks!
Also, I am using Cucumber + Selenium + Java in IntelliJ IDEA, the synthaxis changes just a bit, but it is still different from the codes I tend to find in this forum. Hence, why I am asking for a specific solution to my problem.
You have to make the dropdown visible first.
As in Selenium you can't just hover an element, you will have to do it all in one go.
Check this: How to perform mouseover function in Selenium WebDriver using Java?
Actions action = new Actions(webdriver);
WebElement button = webdriver.findElement(By.class("account-link"));
action.moveToElement(button).moveToElement(webdriver.findElement(By.linkText("Registry Settings")).click().build().perform();
You may have to wait in between for the dropdown to appear. I have not tested the code, you will probably have to fix it before it works.
As you have mentioned when you put your mouse over to a button, it will show the Dropdown container.
Same can be automated with the help of selenium like this : (I am assuming account is a button )
Actions action = new Actions(driver);
action.moveToElement(driver.findElement(By.linkText("Account"))).build().perform();
Now your drop-down is expanded or visible in UI, and you want to click on Registry Settings . Since your drop down is not made using Select options tags which are available in HTML. You can't use Select class from Selenium.
You will have to store every elements which are present in drop down to a list. and then based on some condition , you can click on your desire element.
Code:
List<WebElement> options = driver.findElements(By.cssSelector("div.account-dropdown__container ul li"));
for(WebElement option : options) {
if(option.getText().trim().contains("Registry Settings")) {
option.click();
}
}
Hope this will help.

How to switch to Md-dialog in selenium?

I am having an issue in regards to a md-dialog pop-up in an angularJS app that I am unable to click in selenium. When i click a button the dialog box appears and becomes the active element on the screen, darkening the background. I have tried switchTo with active element, frame, alert and none of these seem to work. My most recent attempt was trying to swap windows using the below code:
winHandleBefore = driver.getWindowHandle();
Set<String> numOfWindows = driver.getWindowHandles();
System.out.print(numOfWindows.size());
for(String winhandle : driver.getWindowHandles())
{
driver.switchTo().window(winhandle);
report.updateTestLog("Switched to window", "", Status.PASS);
}
the S.o.p for the size is always outputted as 1. There is a wait in after the initial button is clicked before the popup appears to assure it has the proper time to appear. Not sure what else there is to do, I have been scouring the internet for an answer and I haven't come across anything that will let me click the elements in that popup
Edit: This is the html for the md-dialog
<md-dialog class="quote _md md-transition-in" aria-label="Summary" role="dialog" tabindex="-1" id="dialogContent_78" aria-describedby="dialogContent_78" style="">
Edit 2: Forgot to say, I am trying to click a button inside the md-dialog popup
Edit 3: After reviewing the code a bit more I noticed that the md-dialog popup has a container div that is taking up the entire screen and that it is also calling in html from another file. The container div html:<div class="md-dialog-container ng-scope" tabindex="-1" style="top: 972px; height: 769px;">
In case someone has same issue the pop-up was registering as hidden even though it had appeared on screen, so any attempt to click a button was futile as the button was seen as hidden. The workaround was to find the button using the findElement() method, assign it to a variable and then use a javascriptExectutor to click it even though it was hidden. This is not ideal if you are trying to reproduce user input but it is a workaround. Code is below.
WebElement hiddenButton = driver.findElement(uniqueIdentifier);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", hiddenButton);
report.updateTestLog(flowName, uniqueIdentifier + " Hidden Button Pressed", Status.DONE);
Hope this helps anyone who finds it.

Java and Selenium, Clicking element by xpath using contains

I am having a problem clicking an element using Selenium and Java. Here is what the table looks like.
<tr class="even">
<td class="linked-item">
<a title="Imported Gatekeeper" href="/rss-servlet/remotesupport/viewgatekeeper.action?siteId=20">SJHS_OK - Technology Center - (CWx Network Connectivity)</a>
</td>
</tr>
<tr class="odd">
<td class="linked-item">
<a title="Imported Gatekeeper" href="/rss-servlet/remotesupport/viewgatekeeper.action?siteId=26">SJHS_OK - Technology Center - (CWx Network Connectivity) - Backup</a>
</td>
</tr>
I am trying to select the first element SJHS_OK - Technology Center - (CWx Network Connectivity), which is a link to another page. This is only a segment of the code, there is much more than this.
My problem arises when I try to use contains, my xpath looks something like this:
//*[#title='Imported Gatekeeper'][contains(.,'SJHS_OK - Technology Center - (CWx Network Connectivity)')][not(contains(.,'Backup'))]
Sometimes when I put it into firepath it works and finds the element, sometimes it doesn't. Not sure why it doesn't always work.
my code in Java looks like this:
String gatekeeper = "SJHS_OK - Technology Center - (CWx Network Connectivity)"
DRIVER.findElement(By.xpath("//*[#title='Imported Gatekeeper']"+gatekeeper+"[not(contains(.,'Backup'))]")).click();
I am doing it this way so that I can easily change the gatekeeper variable and select a different gatekeeper. The gatekeeper will be pulled from an excel sheet.
Any help with what is wrong here would be appreciated, or maybe another way that I can go about finding the element to click it that can be changed by a variable.
If the xpath sometimes finds and sometimes does not, the problem probably is a missing wait. If the element does not exist or is not visible when you try to click, code will throw an error. Try it:
WebElement yourElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#title='Imported Gatekeeper'][contains(.,'SJHS_OK - Technology Center - (CWx Network Connectivity)')][not(contains(.,'Backup'))]")));
#Edit
As wait is not working and code stills sometimes working and sometimes no, when te page is renderized, there is no a constant number of spaces to fills the text. Try this xpath selector:
//a[#title='Imported Gatekeeper'][text()[contains(., 'SJHS_OK')]][contains(., 'Technology Center')][contains(., 'CWx')][contains(., 'Network')][contains(., 'Connectivity')][not(contains(.,'Backup'))]
Also, try all these selectors in google chrome console when test works and not in chrome console. You can test an xpath using an $x("your xpath here")
You don't have to use xpath for that. If you are using Java8 Try this instead:
driver.findElements(By.tagName("a")).stream()
.filter(td -> !td.getText().contains("Backup"))
.findFirst().get().click();
If the element can be found either way, there can be something underneath that, maybe element wasn't loaded completely?
Can you provide stacktrace when error occurs?
I found that when I copied the SJHS_OK - Technology Center - (CWx Network Connectivity directly out of Xpath then it would work, but if I typed it myself, even if it was exact when compared, it would not work. My conclusion is that it has to do with the spaces. The error would disappear if I cut it down without the spaces.

Categories

Resources