【问题标题】:Selenium How to Find Xpath based on Text conditionsSelenium 如何根据文本条件查找 Xpath
【发布时间】:2021-10-04 20:09:41
【问题描述】:

我一直在学习 selenium 并实现它。我尝试从机器人上传文件。在此期间,我开始知道位置(上传按钮内容)在每次重新加载时都会不断变化。我设法找到了 .jpg 文件的选项,但我无法跟踪 .pdf 文件的上传。下面详细介绍了我的代码。

<div _ngcontent-kkx-c10="" class="form-group document-upload" xpath="1">
    <div _ngcontent-kkx-c10="" class="document-upload-info">
        <span _ngcontent-kkx-c10="" class="document-type">
            KYC Form <!----><span _ngcontent-kkx-c10="" style="margin-left:5px;color:red;">* </span>
        </span>
    </div>
    <!----><!---->
    <label _ngcontent-kkx-c10="" class="btn btn-outline upload-button" style="border: 1px solid transparent; border-radius: 0.25rem; border-color: #0078D7; color: #0078D7 !important;">
        Upload <input _ngcontent-kkx-c10="" hidden="" multiple="" type="file" accept=".pdf">
    </label>
    <!----><!----><!---->
</div>


<div _ngcontent-kkx-c10="" class="form-group document-upload" xpath="1">
    <div _ngcontent-kkx-c10="" class="document-upload-info">
        <span _ngcontent-kkx-c10="" class="document-type">
            Photo  Form <!----><span _ngcontent-kkx-c10="" style="margin-left:5px;color:red;">* </span>
        </span>
    </div>
    <!----><!---->
    <label _ngcontent-kkx-c10="" class="btn btn-outline upload-button" style="border: 1px solid transparent; border-radius: 0.25rem; border-color: #0078D7; color: #0078D7 !important;">
        Upload <input _ngcontent-kkx-c10="" hidden="" multiple="" type="file" accept=".jpg">
    </label>
    <!----><!----><!---->
</div>

<div _ngcontent-kkx-c10="" class="form-group document-upload" xpath="1">
    <div _ngcontent-kkx-c10="" class="document-upload-info">
        <span _ngcontent-kkx-c10="" class="document-type">
            Citizenship   Form <!----><span _ngcontent-kkx-c10="" style="margin-left:5px;color:red;">* </span>
        </span>
    </div>
    <!----><!---->
    <label _ngcontent-kkx-c10="" class="btn btn-outline upload-button" style="border: 1px solid transparent; border-radius: 0.25rem; border-color: #0078D7; color: #0078D7 !important;">
        Upload <input _ngcontent-kkx-c10="" hidden="" multiple="" type="file" accept=".pdf">
    </label>
    <!----><!----><!---->
</div>

我按照以下方式上传图片。

photo_filepath_input_box = driver.find_element_by_xpath("//input[@accept='.jpg']").send_keys(
                    '/home/daniel/Desktop/website.jpg')

在机器人加载过程中,上述三个表单可能出现在任何位置,即Kyc 表单可能会到达末尾(第三个位置)或第二个和类似的休息位置。所以我想知道如果我可以检查条件,比如 KYC 表单文本是否存在,那么我需要单击它正下方的 xpath,即 Label 对此有任何提示吗?

这是第二个文件上传问题。

(+) 按钮点击代码

WebDriverWait(self.driver,5).until(EC.presence_of_element_located((By.XPATH,"//span[contains(text(),'Citizenship Certificates')]/../..//i[@class='fa fa-plus ' ]"))).click()

第二次上传公民船代码。

self.driver.find_element_by_xpath("((//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')])[2]").send_keys('/home/navaraj/Desktop/{}'.format(row[75]))

【问题讨论】:

  • 你能展示一下你尝试了什么吗?

标签: python selenium selenium-webdriver xpath


【解决方案1】:

要通过 Selenium 上传文件,更好的方法是将文件路径直接发送到输入元素,而不是通过单击元素。
在这里,您有 2 个表单,其中包含上传 .pdf 文件输入和用于上传 .jpg 文件的单个输入。
因此,要以 KYC 形式上传 .pdf 文件,您可以这样做:.
假设您的文件位于“C://Downloads//kyc_pdf_file.pdf”中的磁盘上,您可以这样做:

kyc_pdf_file_path = "C://Downloads//kyc_pdf_file.pdf"
driver.find_element_by_xpath("//div[.//span[@class='document-type' and (contains(.,'KYC'))]]//input[@type='file' and(@accept='.pdf')]").send_keys(kyc_pdf_file_path)

将文件上传到公民表格:

citizenship_pdf_file_path = "C://Downloads//citizenship_pdf_file.pdf"
driver.find_element_by_xpath("//div[.//span[@class='document-type' and (contains(.,'Citizenship'))]]//input[@type='file' and(@accept='.pdf')]").send_keys(citizenship_pdf_file_path)

上传“.jpg”时会类似:

jpg_file_path = "C://Downloads//jpg_file.jpg"
driver.find_element_by_xpath("//input[@type='file' and(@accept='.jpg')]").send_keys(jpg_file_path)

UPD
公民证书 pdf 上传的 XPath 是

//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')]

KYC 表格是

//span[contains(text(),'KYC Form')]/../..//input[@type='file' and(@accept='.pdf')]

这是第二个文件上传问题。

(+) 按钮点击代码

WebDriverWait(self.driver,5).until(EC.presence_of_element_located((By.XPATH,"//span[contains(text(),'Citizenship Certificates')]/../..//i[@class='fa fa-plus ' ]"))).click()

第二次上传公民船代码。

self.driver.find_element_by_xpath("((//span[contains(text(),'Citizenship Certificates ')]/../..//input[@type='file' and(@accept='.pdf')])[2]").send_keys('/home/navaraj/Desktop/{}'.format(row[75]))

【讨论】:

  • 查看更新后的答案是否符合您的要求
  • 您是否向我提供了该网站的链接,以便我找到正确且唯一的定位器?
  • 更新了答案。现在它应该工作了。你知道,如果没有看到实际的网页,很难给出正确的定位器:)
  • 这很奇怪。我看不到你的代码,但如果你写得正确,它应该可以工作
  • 主要是给你用的,我不管:)
猜你喜欢
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 2021-05-01
  • 2016-11-26
  • 1970-01-01
  • 2013-10-19
  • 1970-01-01
  • 2022-01-20
相关资源
最近更新 更多