find background image location in url content - java

i want to save the background image of "http://yooz.ir/" to my Disk by a java code. this image changes every few days. it has a download link blue arrow, but i don't find image location to save it by code. how i can find the location of this image in url content by jsoup, htmlunit or etc?

You have the wrong url. Here is the right one: http://imgs.yooz.ir/yooz/walls/yooz-950602-2.jpg

I see this element is populated asynchronously
so you need to use some webdriver automation tool, the most common is selenium
/*
import selenium, you can do it via maven;
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>2.45.0</version>
</dependency>
*/
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class SaveImageFromUrl {
public static void main(String[] args) throws Exception {
// download chrome driver http://chromedriver.storage.googleapis.com/index.html
// or use firefox, w/e u like
System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
WebDriver driver = new ChromeDriver(); // opens browsers
driver.get("http://yooz.ir/"); // redirect to site
// wait until element which contains the download link exist in page
new WebDriverWait(driver, 5).until(new ExpectedCondition<WebElement>() {
#Override
public WebElement apply(WebDriver d) {
return d.findElement(By.className("image-day__download"));
}
});
// get the link inside the element via queryselector
// https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
String img2download = driver.findElement(By.cssSelector(".image-day__download a")).getAttribute("href");
System.out.println("img2download = " + img2download);
//TODO download..
driver.close();
}
}

Related

Unable to click on few items on website

The website I am trying to automate is betting website and I have a scenario to automate a horse betting.
I am using selenium 3.0 with Java
From the site I am able to travel to horse race but unable to select Tomorrow and select the race. I tried using xpath, class and other methods but unable to click on these button.
website is
https://www.williamhill.com.au/
1 step. go to the above url
2. Select horse racing from top left corner or navigate to url (https://www.williamhill.com.au/racing?event=horseracing)
3.Click on Tomorrow I am unable to do this
4. Select on particular race from the table (unable to this too)
package automationFramework;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class horseRacing {
public static void main(String[] args) throws Exception {
String exePath = "D:\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", exePath);
WebDriver driver = new ChromeDriver();
//Launch the Online Store Website
driver.get("https://www.williamhill.com.au/");
Thread.sleep(5);
driver.manage().window().maximize();
String Title = driver.getTitle();
System.out.println(Title);
driver.findElement(By.className("MenuItem_text_N8V")).click();
Thread.sleep(25);
// driver.findElement(By.className("RaceGrid_raceTile_imG RaceGrid_raceDisabled_Q0m")).click();
driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
// Print a Log In message to the screen
System.out.println("Successfully opened the website www.Store.Demoqa.com");
//Wait for 5 Sec
Thread.sleep(5);
// Close the driver
// driver.quit();
}
}
I tried to click "TOMORROW" and succeeded.
package test;
import org.openqa.selenium.By;
import org.junit.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateToAUrl {
#Test
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.williamhill.com.au/racing?event=horseracing");
driver.findElement(By.xpath(".//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
}
}
Dot(.) is missing in your code driver.findElement(By.xpath("//*[#id='app']/div/div[4]/div/div/div/div[2]/div[1]/div/div[2]/div[2]/div[2]")).click();
(Before //*[#id~)

Webdriver throwing "Cannot navigate to invalid URL" error

Webdriver is able to navigate to the base url but I am unable to perform any actions in it like clicking a link or getting the page title. Even the:
driver.getCurrentUrl()
is throwing:
unhandled inspector error: {"code":-32603,"message":"Cannot navigate to invalid URL"
Not sure how to fix it.
Can you check if you have prepended http in your URL. I think that will do to solve your issue in case you did not use it.
Change the navigate part as:
driver.navigate().to("http://www.example.com");
in your second ex too, use the following:
driver.navigate().to("http://www.url.com/");
driver.findElement(By.linkText("Login").click();
Complete source code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.safari.SafariDriver;
public class CheckWebDriver {
private final WebDriver driver = new SafariDriver();
void checkLogin(){
String baseUrl = "http://newtours.demoaut.com/";
driver.get(baseUrl);
System.out.println(driver.getCurrentUrl());
driver.findElement(By.linkText("REGISTER")).click();
}
public static void main(String[] args) {
CheckWebDriver web = new CheckWebDriver();
web.checkLogin();
}
}

Java: How to Scrape Images from Amazon with Selenium?

I'm trying to scrape the 6 images on left side of page from this URL on Amazon using Selenium WebDriver:
http://www.amazon.com/EasyAcc%C2%AE-10000mAh-Brilliant-Smartphone-Bluetooth/dp/B00H9BEC8E
However, whatever I try causes an error. What I've tried so far:
I tried scraping images directly using XPATH and then extracting src using "getAttributes" method. For example, for the 1st image on page the XPATH is:
.//*[#id='a-autoid-2']/span/input
so I tried the following:
String path1 = ".//*[#id='a-autoid-2']/span/input";
String url = "http://www.amazon.com/EasyAcc%C2%AE-10000mAh-Brilliant-Smartphone-Bluetooth/dp/B00H9BEC8E";
WebDriver driver = new FirefoxDriver();
driver.get(url);
WebElement s;
s = driver.findElement(By.xpath(path1));
String src;
src = s.getAttribute("src");
System.out.println(src);
But I'm unable to find source.
Note: This problem occurs only when scraping images from certain types of products. For example, I can easily scrape images from this product using Selenium:
http://www.amazon.com/Ultimate-Unification-Diet-Health-Disease/dp/0615797806/
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class mytest {
public static void main(String[] args) {
// TODO Auto-generated method stub
String path = ".//*[#id='imgThumbs']/div[2]/img";
String url = "http://www.amazon.com/Ultimate-Unification-Diet-Health-Disease/dp/0615797806/";
WebDriver driver = new FirefoxDriver();
driver.get(url);
WebElement s;
s = driver.findElement(By.xpath(path));
String src;
src = s.getAttribute("src");
System.out.println(src);
driver.close();
}
}
This code works flawlessly. It is only when scraping certain products that there seems to be no way around it.
I tried clicking on image which causes an iframe to open but I'm unable to scrape images from this iframe either, even after switching to iframe with:
driver.switchTo().frame(IFRAMEID);
I know I can use the "screenshot" method but I'm wondering if there's a way to scrape the images directly?
Thanks
Try this code
String path = "//div[#id='imageBlock_feature_div']//span/img";
String url = "http://rads.stackoverflow.com/amzn/click/0615797806";
WebDriver driver = new FirefoxDriver();
driver.get(url);
List<WebElement> srcs;
srcs = driver.findElements(By.xpath(path));
for(WebElement src : srcs) {
System.out.println(src.getAttribute("src"));
}
driver.close();
Result
2015-01-23 12:36:14 [main]-[INFO] Opened url: http://rads.stackoverflow.com/amzn/click/B00H9BEC8E
http://ecx.images-amazon.com/images/I/41cOP3mFX3L._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/51YkMhRXqcL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/51nSbXF%2BCTL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/31s%2B31F%2BQmL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/41FmTOJEOOL._SX38_SY50_CR,0,0,38,50_.jpg
http://ecx.images-amazon.com/images/I/41U6qpLJ07L._SX38_SY50_CR,0,0,38,50_.jpg
However, to get Amazon Images, I suggest you to try Amazon API https://affiliate-program.amazon.com/gp/advertising/api/detail/main.html
It's much better.

Unable to find dropdown list element in selenium

i have written a basic test using selenium. The test is failing as the selenium unable to select the item from drop down list.
My select statement is not getting executed. Also i have include this select statement within iframe as this falls under it.
Can you please let me know what is the issue with my select statement.
Here is my code for southwest website:
package Default;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.firefox.FirefoxDriver;
public class FirstWDWithoutRecording {
#Test
public void SouthWestSignUp() throws InterruptedException
{
//Open the FF/Chrome browser
//FirefoxDriver oBrw = new FirefoxDriver();
ChromeDriver oBrw = new ChromeDriver();
//Maximize Browser
oBrw.manage().window().maximize();
//Open/Launch www.southwest.com
System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
oBrw.get("http://www.southwest.com/");
//Click on Sign up and Save
//Recognising
oBrw.findElement(By.linkText("Sign up")).click();
//oBrw.get("http://www.southwest.com/html/email/click_n_save_signup.html?clk=GFOOTER-CNS-ENROLL");
Thread.sleep(5000);
//Enter First Name
oBrw.switchTo().frame(0); //'0' as it is the only iframe on the page, the value is the index of all iframes on the page
//do your login actions
oBrw.findElement(By.xpath("//input[#id='FIRST_NAME']")).clear();
oBrw.findElement(By.xpath("//input[#id='FIRST_NAME']")).sendKeys("abc");
//Enter Last Name
oBrw.findElement(By.id("LAST_NAME")).clear();
oBrw.findElement(By.id("LAST_NAME")).sendKeys("Kish123");
//Enter Email ID
oBrw.findElement(By.id("EMAIL")).clear();
oBrw.findElement(By.id("EMAIL")).sendKeys("abc#Kish123.com");
//Selecting Home Airport
Select uiHomeAp = new Select(oBrw.findElement(By.id("HOME_AIRPORT")));
uiHomeAp.deselectByVisibleText("Atlanta, GA - ATL");
//Accepting Conditions
oBrw.findElement(By.id("IAN")).click();
//Click Submit
oBrw.findElement(By.id("submit")).click();
//after return
oBrw.switchTo().defaultContent();
}
}
As #SiKing had stated, your issue is with your deselectByVisibleText(). You want selectByVisibleText().
uiHomeAp.selectByVisibleText("Atlanta, GA - ATL");
Also wanted to point out that it might be easier if you are just getting started, to check out the getting started with selenium framework. If you're using maven, you could do:
<dependency>
<groupId>io.ddavison</groupId>
<artifactId>getting-started-with-selenium-framework</artifactId>
<version>1.2</version>
</dependency>
then your test would look like this:
#Config(browser = Browser.CHROME, url="http://www.southwest.com/")
public class FirstWDWithoutRecording extends AutomationTest {
public void southWestSignUp() {
click(By.linkText("Sign up"))
.switchToFrame(0)
.setText("input#FIRST_NAME", "abc")
.setText("input#LAST_NAME", "Kish123")
.setText("input#EMAIL", "abc#Kish123.com")
.selectOptionByText("select#HOME_AIRPORT", "Atlanta, GA - ATL")
.check("#IAN") // check the terms and conditions
.click("#submit") // form submitted
.switchToDefaultContent()
;
}
}

how i can take a screenshot of a flash video while its playing using selenium webdriver?

To automate the testing of videos, we decided to capture the screenshots of the video while its playing and compare those screenshots in order to ensure the video getting played. But when i am trying to capture the screenshots of the video player while video is getting played, only the black screen is getting displayed.
We are using selenium web-driver and JAVA for developing the automation scripts. Is there any other way that we can ensure the videos are getting played or not. Is there any flash support needed for taking screenshots of a flash video getting played?
Please help us with this. Is the method we following the correct one for video testing?
Please find my below selenium web-driver code
CODE
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Screenshot {
public static void main(String[] args) throws InterruptedException, IOException
{
FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
FlashObjectWebDriver flashapp=null;
flashapp = new FlashObjectWebDriver(driver,"PlayerPlatformAPI");
driver.get("Flash media player URL");
Thread.sleep(1000L);
WebElement streamurl = driver.findElement(By.name("Stream URL"));
streamurl.clear();
streamurl.sendKeys("video link");
WebElement tune=driver.findElement(By.xpath("//div[#id='outer']//input[#type='button']"));
tune.click();
Thread.sleep(1000L);
driver.findElement(By.id("PlayerPlatformAPI"));
screen.seekTo(50);
flashapp.callFlashObject("SeekTo","50");
int i;
for(i=0;i<4;i++)
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
File file1 = new File("c:\\tmp\\"+i+"_scrnshot.jpeg");
FileUtils.copyFile(scrFile,file1);
Thread.sleep(10000);
}
driver.close();
}
}

Categories