【问题标题】:Unable to find element using xpath in selenium无法在 selenium 中使用 xpath 找到元素
【发布时间】:2017-11-27 16:53:21
【问题描述】:

我正在学习 selenium webdriver 以在 naukri.com 网站上练习编写脚本。

我想单击更新配置文件下左侧的“附加简历”链接,但显示“无法找到元素”。 我试图用 xpath 找到元素

driver.findElement(By.linkText(" Attached Resume")).click();

我什至尝试过上课也有同样的错误。

这里是 HTML 示例

<div id="leftNav_updateProfile" class="leftNavBullet">
<ul>
<li>
<a href="//my.naukri.com/Profile/edit?id=&altresid=">
<a href="//my.naukri.com/Profile/editSummary?id=&altresid=">
<a href="//my.naukri.com/EmploymentDetails/add?id=&altresid=">
<a href="//my.naukri.com/Profile/view/subAction/ar?id=&altresid=#ar">
<span class="gryTxt">• </span>
Attached Resume
</a>
<a href="//my.naukri.com/ITProjects/add?id=&altresid=">
<a href="//my.naukri.com/ITSkills/edit?id=&altresid=">
<a href="//my.naukri.com/EducationDetails/edit?id=&altresid=">
<a href="//my.naukri.com/ProfileShowcase/edit?id=&altresid=">
<a href="//my.naukri.com/OtherInfo/edit?id=&altresid=">
</li>
</ul>
</div>

这是我的脚本

package practice;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebElement;

public class UploadResume {

    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "E:\\geckodriver-v0.16.1-win32\\geckodriver.exe");
         WebDriver driver=new FirefoxDriver();
         driver.get("https://www.naukri.com/");
         driver.findElement(By.xpath("//div[text()='Login']")).click();
         driver.findElement(By.id("eLogin")).sendKeys("xyz@gmail.com");
         driver.findElement(By.id("pLogin")).sendKeys("12345");
         driver.findElement(By.xpath("//button[text()='Login']")).click();
         driver.findElement(By.xpath("//div[@id='leftNav_updateProfile']//a[contains(.,' Attached Resume')]")).click();
         driver.findElement(By.id("uploadLink")).click();
         driver.findElement(By.id("attachCV")).click();

    }

}

【问题讨论】:

  • 你能分享HTML的样品吗?
  • 附加简历前是否需要空格?
  • @nirmala 你能考虑展示你的作品吗?谢谢
  • @DebanjanB 请检查我是否添加了代码

标签: selenium xpath selenium-webdriver


【解决方案1】:

如您所见,锚标记有文本“。”在第一个跨度中,因此链接文本“附加简历”将不起作用。

HTML:

<a href="//my.naukri.com/Profile/view/subAction/ar?id=&altresid=#ar">
<span class="gryTxt">• </span>
Attached Resume
</a>

请使用以下 Xpath:

//div[@id='leftNav_updateProfile']//a[contains(.,'Attached Resume')]

【讨论】:

    【解决方案2】:

    这是您问题的答案:

    这是打开 URL https://www.naukri.com/ 的工作代码,通过有效凭据登录并点击 Attached Resume 链接:

    import java.util.concurrent.TimeUnit;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Q44732783_Naukri {
    
        public static void main(String[] args) throws InterruptedException 
        {
    
    
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver=new FirefoxDriver();
            driver.get("https://www.naukri.com/");
            driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
            driver.findElement(By.xpath("//div[text()='Login']")).click();
            driver.findElement(By.id("eLogin")).sendKeys("unix.dev.qa@gmail.com");
            driver.findElement(By.id("pLogin")).sendKeys("iamthebest");
            driver.findElement(By.xpath("//button[text()='Login']")).click();
            WebElement myelement = driver.findElement(By.xpath("//div[@id='leftNav_updateProfile']//a[contains(.,' Attached Resume') and not(@class='gryTxt')]"));
            myelement.click();
    
        }
    
    }
    

    如果这能回答您的问题,请告诉我。

    【讨论】:

    • 谢谢,但仍然显示“无法定位元素”
    • @nirmala 我遇到了同样的情况,但诱导 implicitlyWait 为我解决了这个问题。增加ImplicitlyWait或添加一些ExplicitWait,代码工作正常。谢谢
    【解决方案3】:

    我使用以下 Xpath 进行了尝试:

    driver.findElement(By.xpath("//a[@href ='//my.naukri.com/Profile/view/subAction/ar?id=&altresid=#ar']")).click();
    

    【讨论】:

      【解决方案4】:

      你能检查一下吗,

      driver.findElement(By.linkText("Attached Resume")).click();
      driver.findElement(By.partialLinkText("Attached Resume")).click();
      driver.fineElement(By.xpath("//a[contains(text(),'Attached Resume')]")).click();
      

      【讨论】:

        猜你喜欢
        • 2021-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-08-16
        • 1970-01-01
        • 2022-07-05
        • 1970-01-01
        相关资源
        最近更新 更多