Im trying to get into java/selenium but i have some problems i cant solve.
I cant interact with the "WEITER" Button from the website Snipes.com (https://www.snipes.com/checkout?stage=payment#payment). It works with every other button but the box of this one looks a little bigger than the button itself. Perhaps this is the problem but i dont know how i can only click the button.
The code im using for this part is following:
TimeUnit.MILLISECONDS.sleep(500); //select Paypal as Payment
selectPayment = driver.findElementByXPath("/html/body/div[3]/div[2]/div/div/div[2]/div[1]/div[3]/div[2]/div/div/form/div/ul/li[1]/div[1]/label");
js.executeScript("arguments[0].scrollIntoView(true);", selectPayment);
js.executeScript("arguments[0].click();", selectPayment);
TimeUnit.MILLISECONDS.sleep(500);
nextButtonCheckout = driver.findElementByXPath("/html/body/div[3]/div[2]/div/div/div[2]/div[1]/div[3]/div[2]/button");
nextButtonCheckout.click(); //click the "WEITER" Button
TimeUnit.MILLISECONDS.sleep(200);
Is there any way to click only the "main" button element instead of the whole thing so i can interact with it?
Error: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[5]/div/div/div/div/div1/button"}
whole error: https://pastebin.com/jki8T8Y4
I want to click the little "x" on the top right of this pop up and sometimes it works but sometimes it doesnt. How can I test if it found the element and if not try again? I use proxies for this and the speeds are different so i cant just wait 5s and then check if it works. Sometimes the element can be found in 2s sometimes in 15s. I need to retry it a few times and my idea is to check it the variable 'selectRegion' if something is stored in it. I tried it with if(selectRegion =! "the output from the element") but i cant compare a WebElement with a String.
the code i use:
//Snipes Region + accept cookies
driver.get("https://www.snipes.com/login");
TimeUnit.SECONDS.sleep(7);
selectRegion = driver.findElementByXPath("/html/body/div[5]/div/div/div/div/div[1]/button");
selectRegion.click(); //click on the "x"
System.out.println(selectRegion); //get the variable output the use it later for the retries
acceptCookies = driver.findElementByXPath("/html/body/div[2]/div[2]/div/div[2]/div[2]/button");
acceptCookies.click();
This is a common problem but does anyone know how i can use user:passw authenticated proxies with selenium? Everytime i start it there is this authentication pop up but i cant interact with it.
Related
It might be something obvious I've missed, but I've been staring at it for so long now that I decided to post it here_
I have this snipppet of markup for a submit button:
<button class="hb-knapp hb-knapp--standard" id="lastOpp_SOSIALE_UTFORDRINGER_03077000045_soker__"
type="button" data-e2e-selector="lastOpp_SOSIALE_UTFORDRINGER_03077000045_soker__"><!----><hb-ikon
class="hb-knapp-ikon" _nghost-rmd-c56=""><span _ngcontent-rmd-c56="" class="hb-ikon hb-ikon--
focusable="false"></span><!----></hb-ikon><!----><!----><span translate="" class="hb-knapp-
tekst">Last opp vedlegg<!----></span><!----><!----></button>
I'm trying to verify the visibility of the button by using "beginning with", since the number towards the end of the id and data-e2e-selector is dynamic.
So I'm trying this:
return driver.findElement(By.cssSelector("[id^=lastOpp_SOSIALE_UTFORDRINGER_]")).isDisplayed();
and this:
return driver.findElement(By.cssSelector("[data-e2e-selector^=lastOpp_SOSIALE_UTFORDRINGER_]")).isDisplayed();
Both yields the "Unable to locate element" error:
Unable to locate element: {"method":"css selector","selector":"[id^=lastOpp_SOSIALE_UTFORDRINGER_]"}
What on earth am I not seeing here? As far as I can see, I'm looking for the exact same locator(s) that are plainly visible in the markup?
EDIT:
I also tried using xpath, like this
return driver.findElement(By.xpath("//*[contains(#data-e2e-selector, 'lastOpp_SOSIALE_UTFORDRINGER_')]")).isDisplayed();
and this
return driver.findElement(By.xpath("//*[contains(#dataid, 'lastOpp_SOSIALE_UTFORDRINGER_')]")).isDisplayed();
with the exact same result. This seems completely bonkers.
EDIT2:
An also, of course, as a last test: The exact id:
boolean is = driver.findElement(By.id("lastOpp_SOSIALE_UTFORDRINGER_28037800130_soker__")).isDisplayed();
also returns "element not found".
The strange thing is - other elements on the same page are both visible and interactable.
EDIT3:
Tried adding a manual WebDriverWait, but even then Selenium insists on not finding the element:
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lastOpp_SOSIALE_UTFORDRINGER_28037800130_soker__")));
or
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='lastOpp_SOSIALE_UTFORDRINGER_13108100073_soker__']")));
(The xpath is copied directly from the browser - Chrome.)
Result:
Expected condition failed: waiting for visibility of element located by By.id: lastOpp_SOSIALE_UTFORDRINGER_28037800130_soker__ (tried for 5 second(s) with 500 milliseconds interval)
And I'm watching the page, staring at the element in question, as the wait times out.
EDIT4: Snapshot of the html markup
Based on https://sqa.stackexchange.com/a/7889 and https://stackoverflow.com/a/39594489, I think the problem is that you need specify that you're looking for a button.
Try the following:
driver.findElement(By.cssSelector("button[id^=lastOpp_SOSIALE_UTFORDRINGER_]"))
I was wondering if there's any solution for getting text of a web element if it's present on screen?
Application which I'm automating right now is sometimes displaying unexpected error alert with error code. I want to create a listener that gets the text of the error alert if it's displayed and prints it into console.
I'm using TestNG framework with Selenium WebDriver and Java.
I tried to find any solution on the web but none answered my problem. Only found a way to take screenshot on test fail which is not really solution to my problem.
Check the length count of the WebElements.If length count more than 0 then fetch the text of the element.Let me know how it goes. check the text if returns null then try innerHTML property.
WebDriverWait wait=new WebDriverWait(driver, 30);
List<WebElement> errelements =wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("xpath of locator")));
if(errelements.size()>0)
{
System.out.println(errelements.get(0).getText());
System.out.println(errelements.get(0).getAttribute("innerHTML"));
}
// opening the base URL
driver1.get(baseUrl+"/");
// opening a new tab
driver1.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND + "t");
driver1.get("my URL");
// getting back to the first tab
driver1.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND, Keys.SHIFT, "{");
// I want to signup by clicking the sign up button
driver1.findElement(By.xpath("/html/body/div[1]/header/div[2]/button")).click();
The error that I get after running is :
"Error communicating with the remote browser. It may have died."
But when I run the same code without the navigation, the button click works fine, then it means there is no problem with the xpath.
The information which browser you are using would be very interesting. And the webdriver doesn't need to control the visbile Tab. So i wouldn't let the Browser change the Tab and I would use the "switchTo" Method of the webdriver.
More information to this topic is here.
As #Kikkirej mentuined, i see no reason to use sendKeys to switch between opened tabs. Use Selenium instead, it is a much better approach.
Edit: in addition, try to provide more information, especially the most basic part - the browser you are automating.
I am trying to retrieve a JSON element, the problem is that in the source code it doesn't exist, but I can find it via inspect element.
I have tried with
C.driver.findElement(By.id("ticket-parsed"))
and via XPath
C.driver.findElement(By.xpath("//*[#id=\"ticket_parsed\"]"));
and I can't find it.
Also
C.driver.switchTo().frame("html5-frame");
System.out.println(C.driver.findElement(By.id("ticket_parsed")));
C.driver.switchTo().defaultContent();
i get
[[ChromeDriver: chrome on XP (1f75e50635f9dd5b9535a149a027a447)] -> id: ticket_parsed]
on
driver.switchTo().frame(0) or driver.switchTo().frame(1)
i get that the frame doesn't exists
and at last i tried
WebElement frame = C.driver.findElement(By.id("html5-frame"));
C.driver.switchTo().frame(frame.getAttribute("ticket_parsed"));
an i got a null pointer exception
Here's an image of the source:
what am I doing wrong?
Well!
The element #ticket-parsed is in iFrame. So, you can click it without getting into an iframe.
Here is the code to switch to iFrame,
driver.switchTo().frame("frame_name");
or
driver.switchTo().frame(frame_index);
In your case,
driver.switchTo().frame("html5-frame");
After switching into the iframe, you can click that element using either XPath or CSS.
C.driver.findElement(By.id("ticket-parsed"))
NOTE:
After completing the operation inside the iframe, you have to again return back to the main window using the following command.
driver.switchTo().defaultContent();
I didn't found a solution with my excising setup,but i did found a js command which gets the object correctly
document.getElementById("html5-frame").contentDocument.getElementById("ticket_parsed")
you can integrate js commands like this
JavascriptExecutor js=(JavascriptExecutor)driver;
js.executeScript(*yourCommandHere*);
if you want to get the output of the command just add the word return before your command (in this specific situation it didn't work but in any other situation it did)
*TypeOfData* foo = js.executeScript(return *yourCommandHere*);
at last because of limited time i had to use unorthodox methods like taking screenshots and comparing the images if they are exactly the same
Thanks for the help
I am working with Selenium, now there is a condition:
when I hit a button in my webpage a window pop up opens up.
Now I have to click a radio button (one out of two, it will work even if we send a TAB ) and then click an OK button. I searched in the net and got to know about "driver.getWindowHandle()".
But I don't have any idea dealing with the newly opened window popup.
Need help in this.
For switching purpose u can use enhanced for loop:
for (String winHandle : objDriver.getWindowHandles()) {
objDriver.switchTo().window(winHandle);
}
So it will switch the control from one driver window to child windows.
To interact with elements on the window try to find element with whatever tool u r using and perform the required action after switching to the window.
To return back to parent window you can use the same loop or use:
driver.switchTo().defaultContent();
Check my answer in this post and also read the comments to help you understand the difference between getWindowHandle() and getWindowHandles()
Java: focus is not on pop-window during window handling
We handled this situation using AutoItX - https://www.autoitscript.com/site/ in our Windows/IE C# project:
AutoItX3 autoIt = new AutoItX3();
var handle = autoIt.WinWaitActive("[window title]", "", 20);
Assert.IsTrue(handle != 0", string.Format("Was not able to find: {0}", [window title]);
autoIt.Send("{ESCAPE}"); // tab may work as well for selection
The pop up was a Windows window, and not part of IE, therefore the WebDriver didn't know about it.
Hope this helps.