2 回答

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊
根據(jù)Class URI的 java 文檔:
public final class URI
extends Object
implements Comparable<URI>, Serializable
Represents a Uniform Resource Identifier (URI) reference.
URI實(shí)例具有以下九個(gè)組成部分:
Component? ? ? ? ? ? ? ?Type
--------------------? ? ------
scheme? ? ? ? ? ? ? ? ? String
scheme-specific-part? ? String
authority? ? ? ? ? ? ? ?String
user-info? ? ? ? ? ? ? ?String
host? ? ? ? ? ? ? ? ? ? String
port? ? ? ? ? ? ? ? ? ? int
path? ? ? ? ? ? ? ? ? ? String
query? ? ? ? ? ? ? ? ? ?String
fragment? ? ? ? ? ? ? ? String
由于您已經(jīng)通過提取了當(dāng)前 urlgetCurrentUrl() ,因此您可以將當(dāng)前 url轉(zhuǎn)換為uri并提取每個(gè)組件,如下所示(使用https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/package-summary.html):
代碼塊:
import java.net.URI;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class A_demo?
{
? ? public static void main(String[] args) throws URISyntaxException?
? ? {
? ? ? ? System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
? ? ? ? ChromeOptions options = new ChromeOptions();
? ? ? ? options.addArguments("start-maximized");
? ? ? ? WebDriver driver = new ChromeDriver(options);
? ? ? ? driver.get("https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/package-summary.html");
? ? ? ? String CurrentURL = driver.getCurrentUrl();
? ? ? ? URI uri = new URI(CurrentURL);
? ? ? ? System.out.println("Scheme: "+uri.getScheme());
? ? ? ? System.out.println("Scheme-specific-part: "+uri.getSchemeSpecificPart());
? ? ? ? System.out.println("Authority: "+uri.getAuthority());
? ? ? ? System.out.println("User-info: "+uri.getUserInfo());
? ? ? ? System.out.println("Host: "+uri.getHost());
? ? ? ? System.out.println("Port: "+uri.getPort());
? ? ? ? System.out.println("Path: "+uri.getScheme());
? ? ? ? System.out.println("Query: "+uri.getQuery());
? ? ? ? System.out.println("Fragment: "+uri.getFragment());
? ? }
}
控制臺(tái)輸出
Scheme: https
Scheme-specific-part: //seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/package-summary.html
Authority: seleniumhq.github.io
User-info: null
Host: seleniumhq.github.io
Port: -1
Path: https
Query: null
Fragment: null
最后,要構(gòu)建生成的 URL,https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.html您可以執(zhí)行以下操作:
driver.navigate().to(uri.getScheme()+"://"+uri.getHost()+"/selenium/docs/api/java/org/openqa/selenium/WebDriver.html");

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
document.domain不返回 http/https,例如,它返回該站點(diǎn)的“stackoverflow.com”。
我會(huì)用你開始的東西,
String CurrentURL = driver.getCurrentUrl();
將該 URL 轉(zhuǎn)換為 a java.net.URI,然后使用.getHost(). 您可以將這一切包裝到類似的方法中
public static String getDomainName(String url) throws URISyntaxException {
URI uri = new URI(url);
String domain = uri.getHost();
return domain.startsWith("www.") ? domain.substring(4) : domain;
}
并稱之為
String domainName = getDomainName(driver.getCurrentUrl());
這個(gè)答案是我得到該方法的地方,它列出了有關(guān)此方法的注意事項(xiàng),但聽起來它可能對(duì)您有用。
添加回答
舉報(bào)