Я хотел бы получить текст с веб-сайта, но у меня проблема с библиотекой WebDriver или Selenium.
Итак, когда я пытаюсь
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.new_t);
WebDriver webDriver = new ChromeDriver();
webDriver.get("https://en.wikipedia.org/wiki/Main_Page");
WebElement webElement = webDriver.findElement(By.id("mp-left"));
textView.setText(webElement.getText());
}
Мои библиотеки:
implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-3'
implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.13-rc-1'
androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'
}
Я ожидаю, что результатом будет текст из Википедии, но я получаю, что сборка прошла успешно, и при запуске приложения происходит сбой.
LogCat:
019-11-03 14:34:48.050 17419-17419/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 17419
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2914)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
at java.net.InetAddress.getByName(InetAddress.java:1108)
at java.net.InetSocketAddress.<init>(InetSocketAddress.java:235)
at org.openqa.selenium.net.PortProber.checkPortIsFree(PortProber.java:112)
at org.openqa.selenium.net.PortProber.findFreePort(PortProber.java:61)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:359)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:100)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:105)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:28)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2894)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6692)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)