如何在Selenium中找到div格式的元素。出现错误无法使用xpath表达式定位元素

时间:2018-11-27 08:55:30

标签: selenium selenium-webdriver selenium-chromedriver

下面是代码:

def _get_unique_code(self):
    """

    To be used only once in the save method. It creates the unique code.

    """
    import random
    self.code = f"{self.publication.pub_id}-{self.language.upper()}-{self.format.upper()}-{random.randint(1,10001)}"
    while Document.objects.filter(code=self.code).exists():
        self.code = f"{self.publication.pub_id}-{self.language.upper()}-{self.format.upper()}-{random.randint(1,10001)}"
    return self.code

def save(self, *args, **kwargs):
    if not self.code:
        self.code = self._get_unique_code()
    super(Document, self).save()

我在硒中尝试了以下代码:

<div class="footer-bottom-left">
    <div class="campaignUser">Campaign User</div>
    <div class="callLogLookUp">Call Log Look Up</div>
</div>

enter image description here

1 个答案:

答案 0 :(得分:1)

您错过了班级名称中的破折号。另外,您需要删除第二个点(也不需要第一个)。因此,而不是

.//div[@class='footer-bottomleft'].//div[@class='callLogLookUp']

尝试

//div[@class='footer-bottom-left']//div[@class='callLogLookUp']

您可能还需要实现ExplicitWait

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='footer-bottom-left']//div[@class='callLogLookUp']"))).click();