亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

附加到存儲在變量中的域

附加到存儲在變量中的域

嚕嚕噠 2023-08-04 19:00:32
我正在使用 Selenium Webdriver + Java + TestNG (以防萬一這對任何事情都很重要)我想要做的是獲取當前域,將其存儲為變量,然后將某些內容附加到該變量上并導航到生成的 URL。這就是我正在嘗試的,它獲取了域,但導航失敗。說這是一個無效的論點。String CurrentURL = driver.getCurrentUrl();JavascriptExecutor js = (JavascriptExecutor) driver;String domain = (String) js.executeScript("return document.domain");System.out.println("My Current Domain is: "+domain);driver.navigate().to(domain+"/lightning/o/Lead/home");
查看完整描述

2 回答

?
一只甜甜圈

TA貢獻1836條經驗 獲得超5個贊

根據Class URI的 java 文檔:

public final class URI

extends Object

implements Comparable<URI>, Serializable

Represents a Uniform Resource Identifier (URI) reference.

URI實例具有以下九個組成部分:


Component? ? ? ? ? ? ? ?Type

--------------------? ? ------

scheme? ? ? ? ? ? ? ? ? String

scheme-specific-part? ? String

authority? ? ? ? ? ? ? ?String

user-info? ? ? ? ? ? ? ?String

host? ? ? ? ? ? ? ? ? ? String

port? ? ? ? ? ? ? ? ? ? int

path? ? ? ? ? ? ? ? ? ? String

query? ? ? ? ? ? ? ? ? ?String

fragment? ? ? ? ? ? ? ? String

由于您已經通過提取了當前 urlgetCurrentUrl() ,因此您可以將當前 url轉換為uri并提取每個組件,如下所示(使用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());

? ? }

}

控制臺輸出


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

最后,要構建生成的 URL,https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebDriver.html您可以執行以下操作:


driver.navigate().to(uri.getScheme()+"://"+uri.getHost()+"/selenium/docs/api/java/org/openqa/selenium/WebDriver.html");



查看完整回答
反對 回復 2023-08-04
?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

document.domain不返回 http/https,例如,它返回該站點的“stackoverflow.com”。


我會用你開始的東西,


String CurrentURL = driver.getCurrentUrl();

將該 URL 轉換為 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());

這個答案是我得到該方法的地方,它列出了有關此方法的注意事項,但聽起來它可能對您有用。


查看完整回答
反對 回復 2023-08-04
  • 2 回答
  • 0 關注
  • 214 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號