0
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;


public class Bot {

    /**
     * @param args
     * @throws InterruptedException 
     */
    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        
        WebDriver driver;
        String baseURL = "https://www.phptravels.net/";
        System.setProperty("webdriver.chrome.driver","/home/khawar/workspace/MyTest/chromedriver");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
        driver.get(baseURL);
        List<WebElement> element = driver.findElements(By.xpath("//iframe[@id = 'chat-widget']"));
        Thread.sleep(5000);
        if(element.size()>0){
            element.get(0).click();
        }
        driver.switchTo().frame(1);
        // Enter Name
        driver.findElement(By.id("name")).sendKeys("reretr");
        // Enter Phone Number
        driver.findElement(By.id("o68vveqb3d_157907312787606648")).sendkeys("1234567");
        // Enter Email
        driver.findElement(By.id("email")).sendKeys("test@gmail.com");
        // Click on button
        driver.findElement(By.xpath("//*[@id='pvtkmhumbra']/div/form/div[7]/button/div/span")).click();

    }

}

When I click on Chat Bot icon, it is opened. But I try to enter values by using sendkey then they are not entered into input fields. Any suggestion plz? I don't know what is going wrong with that. Please let me know how I can give input to those fields?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
khawar
  • 112
  • 1
  • 5

2 Answers2

0

Instead of

driver.switchTo().frame(1);

use

driver.switchTo().frame("chat-widget")

and try again.

Veera
  • 66
  • 1
  • 7
0

To send the character sequences within the desired fields of the url https://www.phptravels.net/home, as the the desired elements are within a <iframe> so you have to:

  • Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.

  • Induce WebDriverWait for the desired elementToBeClickable.

  • You can use the following Locator Strategies:

    System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--start-maximized");
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    options.setExperimentalOption("useAutomationExtension", false);
    WebDriver driver =  new ChromeDriver(options);
    driver.get("https://www.phptravels.net/home");
    new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@id='chat-widget']")));
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@aria-label='Open LiveChat chat widget']"))).click();
    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='name']"))).sendKeys("reretr");
    driver.findElement(By.xpath("//label/span[text()='WhatsApp Number:']//following::input[1]")).sendKeys("1234567");
    driver.findElement(By.xpath("//input[@id='email']")).sendKeys("test@gmail.com");
    driver.findElement(By.xpath("//span[text()='Start the chat']")).click();
    
  • Browser Snapshot:

PHPTRAVELLS_CHAT


Reference

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352