I have a problem triying to recognize a xpath from the following web page
http://smartchanneltech.com/top100canalti/
This is the element I want to recognize: https://imgur.com/a/ENOP1
This is the xpath that I´m using:/html/body/div/div/div[1]/h1/a
This is the code I´m using:
public WebElement Empresa (WebDriver driver, int Iterator) {
//WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div/div[1]/h1/a")));
return driver.findElement(By.xpath("/html/body/div/div/div[1]/h1/a"));
}
And, finally, this is the error log: https://imgur.com/a/quFjg
I tried just to driver.findElement(By.xpath("/html/body/div/div/div[1]/h1/a")); but is not working also.
Can you help me with this please?
It is inside a iframe. First switch to the frame and try identifying it.
public WebElement Empresa (WebDriver driver, int Iterator) {
driver.switchTo().frame(0);
String xpath="/html/body/div/div/div[1]/h1/a";
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
return driver.findElement(By.xpath(xpath));
}
If you look into the HTML DOM the WebElement is within an <iframe>. So we need to switch to the <iframe> first with proper WebDriverWait and then locate the WebElement as follows :
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#src='https://www.rise.global/display/top100-canalti/latest/embeddable/cut/stripes']")));
return driver.findElement(By.xpath("div[#class='lb-leaderboard-header']/h1/a[#class='lb-leaderboard-name']"));
I have update the method as below.
Please use this.
public WebElement Empresa (WebDriver driver, int Iterator) {
//WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#src='https://www.rise.global/display/top100-canalti/latest/embeddable/cut/stripes']")));
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
WebElement elem = driver.findElement(By.className("lb-leaderboard-widget-wrapper"));
WebElement elemH1 = elem.findElement(By.tagName("h1"));
WebElement elemIWant = elem.findElement(By.tagName("a"));
System.out.println(elemIWant.getAttribute("innerHTML").toString());
return elemIWant;
}
let me know your feedback.
Related
I am trying to scrape a news webside, but it is not possible to accept the "accept cookies" popup by using the click() method. I can see the button in my HTML code in my browser, but when I use getPageSource() method the code for the button is not included.
Here my code block
public class Webscraping {
public static void main(String[] args) throws Exception{
System.setProperty("webdriver.chrome.driver","C:\\Users\\Marvin\\Desktop\\Webscraping\\chromedriver.exe");
//Pop Up blocken
//ChromeOptions options = new ChromeOptions();
//options.addArguments("disable-popup-blocking");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String url = "https://www.focus.de/";
driver.get(url);
Thread.sleep(5000);
//HTML Code print
System.out.println(driver.getPageSource());
}
}
To click() on the element Akzeptieren within the url https://www.focus.de/ as the the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using cssSelector:
driver.get("https://www.focus.de/");
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='Iframe title']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[title='Akzeptieren']"))).click();
Using xpath:
driver.get("https://www.focus.de/");
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#title='Iframe title']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#title='Akzeptieren']"))).click();
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
After login get successful in gmail, I am not able to click on compose mail button. It gives NoSuchElementException error.
Executemail.java
public void clickin(String objectname) throws Exception{
WebDriverWait wait=new WebDriverWait(driver,20);
WebElement element=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(prop.getProperty(objectname))));
element.click();
}
keyword.java
if(a.get(i).equals("clickin")) {
String Keyword = (String)a.get(i);
String data = (String)a.get(i+1);
String objectname = (String)a.get(i+2);
String runmode = (String)a.get(i+3);
System.out.println(Keyword);
System.out.println(data);
System.out.println(objectname);
System.out.println(runmode);
if(runmode.equals("Yes")) {
key.clickin(objectname);
}
}
enter image description here
Double check your locator, for me the following XPath expression works fine:
//div[#role='button' and normalize-space()='Compose']
It filters the div element by the role attribute and uses normalize-space() function to ignore spaces and line breaks
My suggest would be waiting for the JS on the page to load before calling your click. I would do this with the following method - I have typed this out on my phone so sorry in advanced if any mistakes.
public static void waitForPageToLoad(){
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(new ExpectedCondition<Boolean>(){
#Override
public Boolean apply(WebDriver driver){
return ((JavascriptExecutor)driver).executeScript(“return document.readyState”).equals(“complete”);}});
`
I have tried my best to write a login script in Selenium for the following site https://www.topmba.com/app. Here is my code:
public class TopMba {
String driverPath = "/usr/bin/chromedriver";
WebDriver driver;
String username = "test#gmail.com"; // Change to your username and passwrod
String password = "12345";
// This method is to navigate topmba URL
#BeforeClass
public void init() {
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
driver.navigate().to("https://www.topmba.com");
}
// To log in topmba
#Test
public void login() {
driver.findElement(By.className("tm-user")).click();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.switchTo().frame(driver.findElement(By.xpath("//*[#id=\"tm-modal-frame-nvtfa7vvbm\"]")));
driver.findElement(By.id("edit-user")).sendKeys(username);
driver.findElement(By.id("edit-pass")).sendKeys(password);
driver.findElement(By.id("edit-submit")).click();
driver.switchTo().defaultContent();}
#AfterClass
public void quit() {
driver.close();
}
Here is the Exception :
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="tm-modal-frame-nvtfa7vvbm"]"}
Use the below code:
driver.get("https://www.topmba.com");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
String parentWindowHandle = driver.getWindowHandle();
driver.findElement(By.className("tm-user")).click();
WebElement iframe = driver.findElement(By.xpath("//iframe[contains(#src,'app/sso/user/login')]"));
driver.switchTo().frame(iframe);
driver.findElement(By.id("edit-name")).sendKeys(username);
driver.findElement(By.id("edit-pass")).sendKeys(password);
driver.findElement(By.id("edit-submit")).click();
driver.switchTo().window(parentWindowHandle);
There are a couple of things you need to take care as follows :
To click on the icon with tooltip as Login you have used :
driver.findElement(By.className("tm-user")).click();
If you look at the HTML this Locator Strategy identifies the element uniquely but for a more focused click you need to target the <span> tag which is within an <a> tag which is within the <li> tag with the class attribute you have used. Of-coarse you have induce WebDriverWait.
Once the Sign In Dialog Box opens up you will observe the login fields are within an <iframe>. So you have to induce WebDriverWait for both the cases, once for the frame to be available and for the desired element to be clickable and you can use the following solution :
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\path\\to\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.topmba.com");
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li.tm-user>a.tmba-user>span.fa-img-icons.fa-img-user"))).click();
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[#src='/app/sso/user/login']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.input-block-level.form-text.required.form-control#edit-name"))).sendKeys("khawar");
driver.findElement(By.cssSelector("input.input-block-level.form-text.required.form-control#edit-pass")).sendKeys("khawar");
driver.findElement(By.cssSelector("button.btn.btn-warning.btn-block.button.js-form-submit.form-submit#edit-submit")).click();
Browser Snapshot :
enter image description here
Can you help me to input text on the email box? The email box appears when I click on masuk button on the top, but I can't get to sendkeys on the email box.
H|ere is the url- www.tokopedia.com
And here is the code that does not work
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.tokopedia.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.id("login-ddl-link"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
driver.findElement(By.id("login-ddl-link")).click();
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes213");
WebElement myPassword = driver.findElement(By.id("inputPassword"));
myPassword.sendKeys("tes123");
}
This is because authorization form located inside iframe element. You need to switch to that frame at first and then handle input fields:
...
driver.findElement(By.id("login-ddl-link")).click();
Thread.sleep(2000);
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes123");
...
To switch back you might need to use
driver.switchTo().defaultContent();
P.S. You don't need to click on input field to sent text to it, so driver.findElement(By.id("inputEmail")).click(); is redundant line
use this code it worked just fine for me on Chrome.
public static void main(String [] ar) throws Exception {
System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.tokopedia.com/");
Thread.sleep(3000);
WebElement element = driver.findElement(By.xpath("//*[#id='login-ddl-link']"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
driver.switchTo().frame("iframe-accounts");
WebElement myEmail = driver.findElement(By.id("inputEmail"));
myEmail.sendKeys("tes213");
WebElement myPassword = driver.findElement(By.id("inputPassword"));
myPassword.sendKeys("tes123");
driver.findElement(By.xpath(".//*[#id='global_login_btn']")).click();
}
Scenario:
Login to www.flipkart.com and choose "Samsung" from "Electronics" after successful login.
Now, I need to scroll to the bottom of the page and from the left side, I need click on Availability to choose the "Exclude out of stock option" but
on clicking Availability, I am getting the message
FAILED: Test_Samsung
org.openqa.selenium.WebDriverException: Element is not clickable at point (119, 9). Other element would receive the click: <div class="_1H5F__" data-reactid="10"></div>
Command duration or timeout: 133 milliseconds
//class for successful flipkart login
public class Flipkart_Login
{
#FindBy(xpath="//a[text()='Log In']") WebElement Login_Click;
#FindBy(xpath="//input[#class='_2zrpKA' and #type='text']") WebElement Enter_Email;
#FindBy(xpath="//input[#class='_2zrpKA _3v41xv' and #type='password']") WebElement Enter_Pass;
#FindBy(xpath="//button[#type='submit' and #class='_3zLR9i _1LctnI _36SmAs']") WebElement Login_Button;
#FindBy(xpath="//span[text()='Please enter valid Email ID/Mobile number']") WebElement Blank_Email;
#FindBy(xpath="//span[text()='Please enter Password']") WebElement Blank_Pass;
public void Valid_Login()
{
Login_Click.click();
Enter_Email.sendKeys("abc#gmail.com");
Enter_Pass.sendKeys("abcde");
Login_Button.click();
}
}
//class for choosing Samsung from Electronics menu and clicking Availability
public class Flipkart_Electronics_Samsung_Mobile
{
#CacheLookup
#FindBy(xpath="//a[#title='Electronics']//span[text()='Electronics']") WebElement Electronics_Menu;
#CacheLookup
#FindBy(xpath="//a[#title='Samsung']//span[text()='Samsung']") WebElement Samsung_Mobile_Click;
#CacheLookup
#FindBy(xpath="//div[#class='_3QT2gR _1AgMas']//div[text()='Availability']") WebElement Availability;
#CacheLookup
#FindBy(xpath="//div[#class='_1p7h2j']") WebElement Exclude_Out_Of_Stock;
public void Choose_Samsung_Mobile()
{
WebDriverWait wait = new WebDriverWait(driver, 30);
Actions act = new Actions(driver);
act.moveToElement(Electronics_Menu).perform();
act.click(wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#title='Samsung']//span[text()='Samsung']")))).build().perform();
WebElement Availability = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='_3QT2gR _1AgMas']//div[text()='Availability']")));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].scrollIntoView(true);", Availability);
Availability.click();
}
}
//class which calls the methods from above two classes
public class Flipkart_Electronics_Samsung_Search
{
WebDriver driver;
#Test
public void Test_Samsung()
{
driver = BrowserFactory.getBrowser("Firefox");
driver.get(DataProviderFactory.getConfig().getURL());
Flipkart_Login login = PageFactory.initElements(driver, Flipkart_Login.class);
login.Valid_Login();
Flipkart_Electronics_Samsung_Mobile Samsung = PageFactory.initElements(driver, Flipkart_Electronics_Samsung_Mobile.class);
Samsung.Choose_Samsung_Mobile();
}
}
You can try to click using JavascriptExecutor So you should replace below line :-
js.executeScript("arguments[0].scrollIntoView(true);", Availability);
Availability.click();
To
js.executeScript("arguments[0].click()", Availability);
Element is not clickable at point (119, 9). Other element would receive the click: Command duration or timeout: 133 milliseconds
It clearly says, the element we want to click is hidden by some other element div in this case, which would receive the click.
I think it is problem with UI which shouldn't hide the element, but you can try few things :
1. Maximize the window of browser from webdriver to see if element is still hidden
driver.manage().window().maximize()
Use JavaScript to click element
WebElement element = driver.findElement(By.<locator>);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click()", element)