当子元素使用硒与文本匹配时,选择父子元素

时间:2019-05-20 00:20:43

标签: python selenium

我有这样的html:

import random

X = 0

while X<2000:

    House_deal = [random.randint(0,9) for House_deal in range(5)]
    Player_deal = [random.randint(0,9) for House_deal in range(5)]
    print("House numbers: ", House_deal)
    print("Player numbers: ", Player_deal)
    print(" ")
    X += 1

我要选择与“ Bar”匹配的卡片,然后单击“ View Item”链接。我尝试过

<div class="card">
  <div>Foo</div>
  <a>View Item</a>
</div>
<div class="card">
  <div>Bar</div>
  <a>View Item</a>
</div>

但是我得到了错误:

cards = browser.find_elements_by_class_name('card') for card in cards: if card.find_element_by_partial_link_text('Bar'): item_anchor = card.find_element_by_partial_link_text('View Item') item_anchor.click()

4 个答案:

答案 0 :(得分:0)

尝试在xpath下方使用EC。

选项1:

检查链接是否存在,然后单击(如果您要查找任何特定的链接,则可以在xpath中添加要链接的属性)

link =WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//div[@class='card' and div[normalize-space(.)='Bar']]/a")))
if (link):
    link.click()

选项2:

使用不同的xpath和len

links =WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,"//div[@class='card']/div[normalize-space(.)='Bar']/following-sibling::a[normalize-space(.)='View Item']")))
if len(links)>0:
    links[0].click()

选项3:

如果不确定BA之间是否存在任何级别,可以使用下面的xpath。

links =WebDriverWait(driver,10).until(EC.presence_of_all_elements_located((By.XPATH,"//div[normalize-space(.)='Bar']/ancestor::div[@class='card']//a[normalize-space(.)='View Item']")))
if len(links)>0:
    links[0].click()

答案 1 :(得分:0)

如果要单击View Item中的Bar,可以直接使用此xpath:

//div[text()='Bar']/following-sibling::a[text()='View Item']  

但是,如 @supputuri

所述,引入webdriver wait将是实现稳定性的一个好主意。

答案 2 :(得分:0)

There are two ways to handle this situation based on your UI behavior:

1) If UI is fixed, use this xpath to identify and use click() to click on it.

//*[@class='card']/div[.='Bar']/following-sibling::a

2) If you are taking data from any external sources (like Database or Excel), pass your expected value (like Bar or Foo) as a parameter to the xpath method like below:

Define a class called Element like as below:

public class Element {

    private WebElement element;
    private WebDriver driver;
    private String xpath;

//  Constructor will wrap web element into Object and will allow to use any of the method declared below
    public Element(String xpath) {
        this.driver = new ChromeDriver();
        this.xpath = xpath;
        this.element = this.driver.findElement(By.xpath(this.xpath));
    }

    public void click() {
        this.element.click();
    }
}

Create POM class and write a methods like below:

public class PageObjectClass {

        private Element elementToClick(String value) {
            return new Element("//*[@class='card']/div[.='" + value + "']/following-sibling::a");
        }

        public void clickOnViewItemsLink(String value) {
            this.elementToClick(value).click();
        }
    }

By this way, you can click on any of View Item link just by passing value as a parameter

答案 3 :(得分:0)

这可以通过使用正确的xpath通过单行代码来实现。

driver.findElement(By.xpath("//div[text()='Bar']/following-sibling::a[text()='View Item']")).click();