【问题标题】:Selenium: How to use findElement(By.cssSelector) to extract text from HTMLSelenium:如何使用 findElement(By.cssSelector) 从 HTML 中提取文本
【发布时间】:2022-01-03 04:23:02
【问题描述】:

您好,我希望您使用 driver.FindElementsByCssSelector(''); 从 Class messenger 获取文本 @user( span class="user" )

因为相同的代码在同一个页面,但不同的起始 div class="messenger"

注意:该代码不能在网站上更改

<div class="messenger">
    <div class="two"> 
        <div class="three">  
            <a class="complex" rel="user" href="https://example.com"> 
                <img class="avatar pull-right" src="https://example.co/a.jpg" alt="avatar"> 
            </a>    
        </div> 
        <div class="webs">      
            <a class="link-complex" rel="user" href="afaq"> 
                <b class="link-complex-target">website</b> 
                <span class="user">@user</span> 
            </a>
        </div>
    </div> 
</div>

【问题讨论】:

    标签: c# selenium selenium-webdriver xpath css-selectors


    【解决方案1】:

    要提取文本@user,因此您必须将WebDriverWait 诱导为所需的ElementToBeClickable(),您可以使用以下任一Locator Strategies

    • 类名

      Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.ClassName("user"))).Text);
      
    • CssSelector

      Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//span.user"))).GetAttribute("value"));
      
    • XPath

      Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//span[@class='user']"))).GetAttribute("value"));
      

    【讨论】:

      猜你喜欢
      • 2020-11-05
      • 2020-01-01
      • 2020-11-11
      • 2019-02-04
      • 2020-09-24
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-07-13
      相关资源
      最近更新 更多