简体   繁体   English

Try-Catch无法捕获“ System.setProperty”中的错误

[英]Try-Catch does not catch errors from “System.setProperty”

I am using Selenium and Java to write a test, I added the code below to see if I am using a wrong Chrome driver, 我正在使用Selenium和Java编写测试,我在下面添加了代码以查看是否使用了错误的Chrome驱动程序,

  try {
    System.setProperty("webdriver.chrome.driver", "Chrome Drivers\\chromedriverLinux32");
        } catch (Exception e) { 
            System.out.println(1);
            e.printStackTrace();    
        }

but whenever it happens, the test throws: 但是无论何时发生,测试都会抛出:

Jan 14, 2016 11:04:25 AM org.openqa.selenium.os.UnixProcess checkForErrorSEVERE: org.apache.commons.exec.ExecuteException: Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "C:\Users\sina\Desktop\WorkSpace\CowZAllPages\Chrome Drivers\chromedriverLinux32" (in directory "."): CreateProcess error=193, %1 is not a valid Win32 application)

But the catch does catch any errors ( System.out.println(1); is not seen). 但是catch确实捕获了任何错误(未看到System.out.println(1); )。

There's nothing wrong with your code in the way you assign the Chrome Path. 分配Chrome路径的代码没有错。 The System literally gets that string. 系统从字面上获取该字符串。
You'll want to have a look at the driver initialization and put a try-catch there, in order to catch such exceptions. 您将需要查看驱动程序初始化并在其中放置try-catch,以捕获此类异常。

Update: 更新:
Perhaps you need to revise this use case - in the sense that you can have 3 ChromeDriver paths and use the right one depending on the platform. 也许您需要修改此用例-从某种意义上说,您可以拥有3个ChromeDriver路径,并根据平台使用正确的路径。

System.getProperty("os.name")

This way, you'll always use the right ChromeDriver. 这样,您将始终使用正确的ChromeDriver。

The last part of the error says "not a valid Win32 application". 错误的最后一部分显示“不是有效的Win32应用程序”。 Is "Chrome Drivers\\chromedriverLinux32" an .exe? “ Chrome Drivers \\ chromedriverLinux32”是.exe吗? Do you need to add the file extension? 您需要添加文件扩展名吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM