Unable to run javaScript on change Function though Web Scrapping Using Selenium With Java

时光毁灭记忆、已成空白 提交于 2020-08-26 07:19:25

问题


My task:

1)I have to go this url : https://www1.nseindia.com/live_market/dynaContent/live_watch/pre_open_market/pre_open_market.htm

2)Select the option "FO Stocks" from selection box so that New table of FO Stocks will appear.

3)After that Sort the table in decending by click on "FFM Column" which has href table header.

Extract Top 5 Rows of the Table.

My Approach:

Selenium Webdriver using Java

1)I have sucessfully opened the url in FireFox tried with Chrome which is talking to long to load the whole page.

2)After Loading of webpage I changed the Selection Box Value to FO by using ID of select box and value of Option. the following code Snippet shows this.

Select dropdown = new Select(driver.findElement(By.id("selId")));
dropdown.selectByValue("fo");

Problem: The table doesn't update value according to the selected option might be because onchange() function of javascript doesn't execute/enabled after changing value of selection box on that page.

3)How to click on this Column using Selenium Webdriver (Any tips is Appreciated).

Here is the Code

public class FirstSeleniumTest {
WebDriver driver;
public void waitForLoad(WebDriver driver) {
System.out.print("waiting for javascript sucessfully");
new WebDriverWait(driver, 120).until((ExpectedCondition<Boolean>) wd ->
((JavascriptExecutor) wd).executeScript("return document.readyState").equals("complete"));
}
public void launchBrowser() {
// System.setProperty("webdriver.chrome.driver", "C:\\Downloads\\chromedriver_win32\\chromedriver.exe");
// driver = new ChromeDriver();
System.setProperty("webdriver.gecko.driver","C:\\Downloads\\geckodriver-v0.26.0-win64\\geckodriver.exe");
DesiredCapabilities dcap = new DesiredCapabilities();
dcap.setCapability("pageLoadStrategy", "eager");
FirefoxOptions opt=new FirefoxOptions();
opt.merge(dcap);
WebDriver driver = new FirefoxDriver(opt);
System.out.print("Driver loaded sucessfully");
     driver.get("https://www1.nseindia.com/live_market/dynaContent/live_watch/pre_open_market/pre_open_market.htm");
waitForLoad(driver);
Select dropdown = new Select(driver.findElement(By.id("selId")));
waitForLoad(driver);
dropdown.selectByValue("fo");
waitForLoad(driver);
}
public static void main(String[] args)
{
FirstSeleniumTest ft=new FirstSeleniumTest();
ft.launchBrowser();
}
}

回答1:


Instead of selecting, trying removing the attribute of the first element and add "selected" attribute to the option you want to select through jquery/javascript. After that trigger the element's on change method.




回答2:


Try triggering that with:

$('selId').trigger('change')



回答3:


Follow the below steps which have worked for me

  1. Launch the chrome manually from custom user directory as shown in image

    1. Go to chrome browser exe path in windows will be like C:\Program Files (x86)\Google\Chrome\Application\
    2. Open CMD in this path and execute the below command chrome.exe --user-data-dir="C:\selenum\AutomationProfile" where C:\selenum\AutomationProfile is custom user data directory
    3. Now launch manually your URL which is to be automated in this browser
    4. Wait until webpage loads completely and close the browser
  2. Now from selenium launch the browser from this custom user-data-dir using the code below

public class Test {

    
    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", "Your Driver path");
        ChromeOptions option=new ChromeOptions();
        //arguments to launch chrome with Custom user dir path
        option.addArguments("--user-data-dir=C:\\selenum\\AutomationProfile\\");
        WebDriver driver = new ChromeDriver(option);
        
        driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
        driver.get("https://www1.nseindia.com/live_market/dynaContent/live_watch/pre_open_market/pre_open_market.htm"); // WebElement
        Select dropdown = new Select(driver.findElement(By.id("selId")));
        dropdown.selectByValue("fo");
        Thread.sleep(2000);
        dropdown.selectByValue("sme");
        Thread.sleep(2000);
        dropdown.selectByValue("niftybank");
        Thread.sleep(2000);
        System.out.println(driver.getTitle());
        
    }

}

Handle dropdowns with required webdriver wait to scrap the table data and using Thread.sleep is not recommended one.

Reason behind launching chrome in custom user profile is to ensure fast website loading in chrome and also website is blocking the client browser when it is launched from selenium that's the reason it takes time to load in chrome automated browser and no data change based on the dropdown values

Hope this will be helpful



来源:https://stackoverflow.com/questions/63420617/unable-to-run-javascript-on-change-function-though-web-scrapping-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!