I have tried, quite a lot and I am not even getting any sort of errors but it is not printing anything (I want it to print the title of the page)
WebDriver driver = new HtmlUnitDriver();
WebElement element = driver.findElement(By.cssSelector("a[href*='Alerts.htm']"));
element.click();
System.out.println(driver.getTitle());
Here is the HTML code (the part I wish to click), there is a title for both , the page I want to click and the current page.
<li title="Alerts"><span>Alerts</span></li>
I am not any errors but it should print the title, which it is not doing.
I have followed many sorts of instructions found here and on the web.
Things I have tried so far:
By locator = By.xpath("//li[#title='Alerts']/a");
WebElement element = driver.findElement(locator);
element.click();
WebElement element = driver.findElement(By.partialLinkText("Alert"));
element.click();
Where am I going wrong?
The title of an HTML document is defined within a <title> tag, typically in the <head> section.
This is the title that the getTitle method returns.
See http://www.w3schools.com/tags/tag_title.asp.
I am not sure of this. But I think HTMLUnitDriver is a headless browser instance. Kindly try in another browser, firefox perhaps.
WebDriver driver = new FirefoxDriver();
You first need to open a page in a browser!
WebDriver driver = new ...
driver.get(url) // THIS LAUNCHES AN ACTUAL BROWSER
// now you can actually do things
driver.findElement ...
driver.getTitle()
driver.quit() // TO DISMISS THE BROWSER
Related
I'm practicing and trying to parse behance.net to retrieve .jpg files.
First, I tried with JSOUP, but I only receives and JS code without any useful code. Then i tried with selenium:
System.setProperty("webdriver.chrome.driver", "S:\\behance-id\\src\\main\\resources\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.behance.net/gallery/148589707/Hercules-and-Randy");
String str = driver.getPageSource();
And I got same result. Through Google Chrome inspect option I found what I need:
But I cannot acces to this source page via Selenium and JSOUP and other instruments.
I only receive this with <script> tags:
Is it possible?
That page is loading its resources dynamically, after the original HTML, so you should use waits in Selenium. This is the Java example for waiting an element to be loaded in page, from the documentation:
WebDriver driver = new ChromeDriver();
driver.get("https://google.com/ncr");
driver.findElement(By.name("q")).sendKeys("cheese" + Keys.ENTER);
// Initialize and wait till element(link) became clickable - timeout in 10 seconds
WebElement firstResult = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//a/h3")));
// Print the first result
System.out.println(firstResult.getText());
The documentation can be found at https://www.selenium.dev/documentation/webdriver/waits/
i have to automate this page using java ,and selenium.
I must click on the link which has label terms and conditions it shows up a box, and then i need to navigate it down, and click on Agree button.
I have already tried:
driver.click("//*[#id=\"field_terms\"]/td[2]/div/div/label[1]/a"); // Click on the link
it opens the box for me, but i stuck after it.
I have 2 problems:
How to scrol down the pop up?
How to click on the Agree button?
Update:
Regarding the scroll problem i used the below method which does not work:
public void scrollDown() {
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("arguments[0].scrollTop = arguments[1];","window.scrollBy(0,450)", "");
}
Try using the CSS Selector to locate the I Agree button
'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'
It worked in my system. I am not a Java person, this is the code I wrote in python for your reference
driver = Chrome()
driver.get('https://signup.insly.com/signup')
terms_and_conditions = driver.find_element_by_link_text('terms and conditions')
terms_and_conditions.click()
import time
time.sleep(2)
i_agree = driver.find_element_by_css_selector(
'div[class="ui-dialog-buttonset"]>button[btnclass="primary"]'
)
i_agree.click()
Let's start with the scroll: just use JavaScript...
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");
Or you can just scroll up to the element needed...
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", your_WebElement);
For the click 'I agree' use XPATH like this:
"//*[text()='I AGREE']" then just preform click()
Hope this helps you!
Here is the code in Java [Tested & worked].
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/src/drivers/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://signup.insly.com/signup");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.linkText("terms and conditions")).click();
//Click the I AGREE button
WebElement btnIagree = driver
.findElement(By.xpath("//button[#btnclass='primary' and contains(text(),'I agree')]"));
btnIagree.click();
//Verify the check box for T&C is checked
WebElement chkbxTnC=driver.findElement(By.xpath("//*[#id='agree_termsandconditions']/../span[#class='icon-check-empty icon-check']"));
Assert.assertTrue(chkbxTnC.isDisplayed());
The problem is, as soon as you click on the T&C link, it takes few seconds to load the page and since a wait is needed before hitting the I AGREE Button. Apparently, the I AGREE Button is enabled and clickable without the need of scrolling down, so scrolling is not needed here.
I want to scroll down my web page. What Java command should I use in Selenium?
Scrolling down a web page is not a valid usecase which can be validated perhaps you want to scroll down to bring a WebElement within the Viewport to interact with it. To achieve that you can use the executeScript() method as follows :
((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", element);
Use the below code and try,
JavascriptExecutor js = (JavascriptExecutor) driver;
// Launch the application
driver.get("http://demo.guru99.com/test/guru99home/");
//This will scroll the web page till end.
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Please refer the code below :
1) Using Action class****
WebDriver driver = new ChromeDriver();
//Creating an object 'action'
Actions action = new Actions(driver);
//open SoftwareTestingMaterial.com
driver.get(Site URL); // Give site URL as name of the web page
//sleep for 3secs to load the page
Thread.sleep(3000);
//SCROLL DOWN
action.sendKeys(Keys.PAGE_DOWN).build().perform();
Thread.sleep(3000);
//SCROLL UP
action.sendKeys(Keys.PAGE_UP).build().perform();
There is somthing wrong with the page I want to test.
My first try:
When I clicked manually on a button, then I will be forwarded normally on the next page.
When I tried to click on the same button with selenium, then I get an error page "Sorry...something gone wrong...blabla". I think this problem can only solve the developer team of the page.
By book = By.cssSelector("#button\\.buchung\\.continue");
//By book = By.cssSelector("button.buchung.continue");
//By book = By.xpath("//*[#id='button.buchung.continue']");
WebElement element= ConfigClass.driver.findElement(book);
element.click();
But I want to try a workaround:
I clicked on the same button with JQuery.
I opened my chrome console and execute the button with:
jQuery('#button\\.buchung\\.continue').click()
How can I execute this JQuery expression in my selenium code?
I tried this, but without success:
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("jQuery('#button\\.buchung\\.continue').click()");
Use $
je.executeScript("$('#button\\.buchung\\.continue').click()");
jQuery("selector") will return you a list.
I think you have to call click() on the element at index 0 (Assuming exactly one element satisfies the selector)
Code:
je.executeScript("jQuery('#button\\.buchung\\.continue')[0].click()");
You were pretty close. If the cssSelector is uniquely identifying the WebElement you can use the following code block :
By book = By.cssSelector("#button\\.buchung\\.continue");
WebElement element= ConfigClass.driver.findElement(book);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
Is there a way in Selenium (java) to get the "page source" as shown on the elements page (F12) in chrome.
I have to test an application the page is heavily modified by javascript. I already tried:
String html = (String)((JavascriptExecutor)driver).executeScript("return document.getElementsByTagName('html')[0].innerHTML");
and
String html = driver.getPageSource();
but both show the "effective" html.
Try to wait until the expected elements are loaded - see https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
E.g.
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).
.until(ExpectedConditions.presenceOfElementLocated(By.id("myDynamicElement")));