第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

附加到存儲(chǔ)在變量中的域

附加到存儲(chǔ)在變量中的域

嚕嚕噠 2023-08-04 19:00:32
我正在使用 Selenium Webdriver + Java + TestNG (以防萬一這對(duì)任何事情都很重要)我想要做的是獲取當(dāng)前域,將其存儲(chǔ)為變量,然后將某些內(nèi)容附加到該變量上并導(dǎo)航到生成的 URL。這就是我正在嘗試的,它獲取了域,但導(dǎo)航失敗。說這是一個(gè)無效的論點(diǎn)。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貢獻(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");



查看完整回答
反對(duì) 回復(fù) 2023-08-04
?
qq_笑_17

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ì)您有用。


查看完整回答
反對(duì) 回復(fù) 2023-08-04
  • 2 回答
  • 0 關(guān)注
  • 193 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)