硒无法通过链接文本找到元素

时间:2018-12-31 20:07:32

标签: python selenium google-chrome

我尝试使用     driver.find_element_by_partial_link_text('2019') 但我收到一条错误消息,说它找不到元素。我也尝试过使用find_element_by_link_text('')并使用整行,但是它行不通。 想法?

driver.find_element_by_partial_link_text('2019').click()

这就是我一直在尝试而没有做的事情。 这是HTML页面:

<div class="rowOf" id="tableRow1">
    <div class="tableD">
        <div class="productDiv" id="productDiv92195">
            <h2 class="productTitle" id="productTitle92195" onclick="goToProduct(0)">2019 Wall Calendar by Camoleaf</h2>
            <img class="productImage" src="https://images-na.ssl-images-amazon.com/images/I/91j3pmPYDOL.jpg" onclick="goToProduct(0)">
            <hr>
            <h4 class="normalPrice" id="normalPrice0" onclick="goToProduct(0)">
                Normally: <span class="currency">$  </span>16.95
            </h4>
            <h4 class="promoPrice" style="margin:2.5px auto;" id="promoPrice92195" onclick="goToProduct(0)">
                Your Amazon Price: <span class="currency">$  </span>1.70
            </h4>
            <h3>Your Total: <span class="currency">$  </span>1.70</h3>
            <p class="clickToViewP" id="cToVP92195" onclick="goToProduct(0)">Click to view and purchase!</p>
        </div>
    </div>
    <div class="tableD">
        <div class="productDiv" id="productDiv69354">
            <h2 class="productTitle" id="productTitle69354" onclick="goToProduct(1)">Pure Lyft Energy Drink Mix (4 Pack) by PURELYFT</h2>
            <img class="productImage" src="https://images-na.ssl-images-amazon.com/images/I/81kCgs96Z0L.jpg" onclick="goToProduct(1)">
            <hr>
            <h4 class="normalPrice" id="normalPrice1" onclick="goToProduct(1)">
                Normally: <span class="currency">$  </span>9.99
            </h4>
            <h4 class="promoPrice" style="margin:2.5px auto;" id="promoPrice69354" onclick="goToProduct(1)">
                Your Amazon Price: <span class="currency">$  </span>0.99
            </h4>
            <h3>Your Total: <span class="currency">$  </span>0.99</h3>
            <p class="clickToViewP" id="cToVP69354" onclick="goToProduct(1)">Click to view and purchase!</p>
        </div>
    </div>
    <div class="tableD">
        <div class="productDiv" id="productDiv79478">
            <h2 class="productTitle" id="productTitle79478" onclick="goToProduct(2)">Multi-Purpose Calf Compression Sleeves by DS Sports</h2>
            <img class="productImage" src="https://images-na.ssl-images-amazon.com/images/I/91U7ExY-SfL.jpg" onclick="goToProduct(2)">
            <hr>
            <h4 class="normalPrice" id="normalPrice2" onclick="goToProduct(2)">
                Normally: <span class="currency">$  </span>12.95
            </h4>
            <h4 class="promoPrice" style="margin:2.5px auto;" id="promoPrice79478" onclick="goToProduct(2)">
                Your Amazon Price: <span class="currency">$  </span>5.05
            </h4>
            <h3>Your Total: <span class="currency">$  </span>5.05</h3>
            <p class="clickToViewP" id="cToVP79478" onclick="goToProduct(2)">Click to view and purchase!</p>
        </div>
    </div>
</div>

2 个答案:

答案 0 :(得分:2)

在示例HTML中,“ 2019”的唯一实例位于<h2>标记中,而不是锚点(<a>)链接中。由于find_element_by_partial_link_text()仅搜索锚标记,因此找不到。

您可以通过XPath搜索以通过部分文本找到任意元素。像这样:

all_matches = driver.find_elements_by_xpath("//*[text()[contains(., '2019')]]")
all_matches[0].click()

该XPath说:

  1. 搜索所有个元素(*
  2. 依次查看每个项目的text()
  3. 如果该text() contains()字符串“ 2019”,则将其添加到匹配项中。

当然,我们只单击匹配的 first 元素。

答案 1 :(得分:0)

代替使用driver.findElement(By.partialLinkText(“ 2019”)); 您应该使用driver.findElement(By.linkText(“ 2019”)); 再次无效,因为许多calander的链接文本为2019。因此,您需要提供一个特定的名称。 例如,我这样做:-

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class AmazonShoping {

    public static void main(String[] args) 
    {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\priyj_kumar\\Downloads\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.amazon.in");
        driver.findElement(By.id("twotabsearchtextbox")).sendKeys("2019 Calander",Keys.ENTER);
        driver.findElement(By.linkText("mapyourmonth Planner Organizer Diary Wall Calendar 2019")).click();
        //checking for a particular boat headphone say Boat BassHeads 900 Wired Headphone with Mic
//      driver.findElement(By.linkText("Boat BassHeads 900 Wired Headphone with Mic")).click();
//      String str = driver.findElement(By.xpath("//*[@id='mp-tfa']/p")).getText();
//      System.out.println(str);
    }

}

这对我来说很好。如果我以某种方式误解了这个问题,请告诉我。