如何通过Selenium和Python单击跨度类文本

时间:2019-02-28 07:17:59

标签: python selenium xpath css-selectors xpath-1.0

我要单击span类中的文本

<div class="u_pg _more_btn_wrapper" style=""> 
<a href="#" onclick="nclk(this, 'sis.moreb', '', '')" class="u_pg_btn _btn_more"> 
<div class="u_pg_wrap"> 
<span class="u_pg_area"> 
<div class="loading _more_btn_loading" style="display: none;"> 
<span class="load_img">로딩중..
</span> 
</div> 
<span class="u_pg_txt _more_txt">더보기</span> 
</span>
 </div>
 </a> 
</div>

单击跨度类“ u_pg_txt _more_txt“>더보기 我在下面尝试了两个不同的代码;

#driver.find_element_by_xpath("//span/span[contains(.,'더보기')]").click()
#driver.findElement(By.partialLinkText("더보기")).click();

和 他们没有工作 帮帮我..

2 个答案:

答案 0 :(得分:0)

创建xpath的另一种方法->

driver.find_element_by_xpath("//span[contains(@class,'u_pg_txt')]").click()

答案 1 :(得分:-1)

点击文本为더보기的元素,您可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.u_pg._more_btn_wrapper > a.u_pg_btn._btn_more span.u_pg_area span.u_pg_txt._more_txt"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='u_pg _more_btn_wrapper']/a[@class='u_pg_btn _btn_more']//span[@class='u_pg_area']//span[@class='u_pg_txt _more_txt']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC