Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

selenium - How to launch a specific URL with Firefox in C#

I'm trying to launch a specific URL using Firefox. But I'm only able to open Firefox browser and not able to launch that URL.

class BrowserHelper
{
    IWebDriver driver;
    string path = Path.Combine(Environment.CurrentDirectory, @"gecko");
    public void Navigate(string url)
    {
        path = path.Replace(@"", @"");
        var driverService = FirefoxDriverService.CreateDefaultService(path);
        driverService.HideCommandPromptWindow = true;
        if (driver == null)
        {
            driver = new FirefoxDriver(driverService);
        }
            driver.Url = url;
            driver.Navigate().GoToUrl(driver.Url);
            driver.Manage().Window.Maximize();
    }
}
class Realtest
{
    BrowserHelper BH = new BrowserHelper();
    public void test()
    {
        string search ="apple";
        BH.Navigate("https://www.google.com/search?q=" + search);
    }
}

And I can only get this page: enter image description here

Here's the final URL I want to launch: https://www.google.com.sg/search?q=apple

Any suggestions? Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have tried the below code (in Java), and it's working fine by launching the browser and loading the URL also.

System.setProperty("webdriver.gecko.driver","Drivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com.sg/search?q=apple");

So I feel the problem is with geckodriver version and FireFox browser installed in your local machine. I would suggest you update FireFox and geckodriver to the latest version.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...