【问题标题】:setAttribute a href in Selenium JavaSelenium Java中的setAttribute a href
【发布时间】:2018-11-15 11:49:06
【问题描述】:

我得到了一个元素的href。之后,我 subString,... 来创建一个新的 href 并希望将其设置回元素。但是 Selenium 没有setAttribute 方法

nextListByNumber = driver.findElement(By.xpath("//*[@id='paginater']//*[text()='"+2+"']"));
String href = nextListByNumber.getAttribute("href");
int manualcode1 = href.lastIndexOf("MANUAL");
int manualcode2 = href.lastIndexOf("/sort");
String manualcode = href.substring(manualcode1, manualcode2);

String hrefNew = "http://localhost/jp/courseassign/course_assign_search_result/"+manualcode+"/sort:User.login_id/direction:asc/page:"+i+"?limit=20";

【问题讨论】:

    标签: java selenium selenium-webdriver setattribute


    【解决方案1】:

    您可以为此使用 JavaScript

    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);", nextListByNumber, "href", hrefNew);
    

    【讨论】:

    • 它不起作用。使用您的代码时,我使用 getAttribute("href") 进行检查,但链接没有更改
    • @AnhPhan 这是因为您进行了更改,驱动程序读取的是原始 html,而不是本地的。它应该将您重定向到新页面。
    • 我们有什么办法让JavascriptExecutor在我们做出改变后自动点击href链接?
    • @AnhPhan 像任何其他元素一样点击它。
    • JavascriptExecutor 执行器 = (JavascriptExecutor) 驱动程序; executor.executeScript("arguments[0].setAttribute(arguments[1], arguments[2]);", nextListByNumber, "href", hrefNew); nextListByNumber.click();但是……我还是卡住了
    猜你喜欢
    • 1970-01-01
    • 2023-02-02
    • 1970-01-01
    • 2023-01-25
    • 2021-01-08
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多