简体   繁体   English

如何使用Selenium和Python在innerText包含前导和尾随空格字符时定位和单击元素

[英]How to locate and click the element when the innerText contains leading and trailing white-space characters using Selenium and Python

I want to locate (and click) the "Reoni" element, but I do not know what function to use it for我想找到(并单击)“Reoni”元素,但我不知道将它用于什么功能

I tried with我试过

driver.find_element_by_class_name("oe_menu_leaf")  

and

driver.find_element_by_class_name("oe_menu_text")

but then selenium raise an error element cant be located, and I tried但随后硒引发了一个错误元素无法定位,我试过

driver.find_element_by_link_text("Reoni")

This is the element I want to locate:这是我要定位的元素:

<a href="/web#menu_id=86&amp;action=99" class="oe_menu_leaf" data-menu="86" data-action-model="ir.actions.act_window" data-action-id="99">
    <span class="oe_menu_text">
    Reoni
    </span>
</a>

and full html:和完整的html:

html

If I was not clear enough or if you needed my code, please let me know.如果我不够清楚或者您需要我的代码,请告诉我。

Try something like this:尝试这样的事情:

Clicking the button点击按钮

From Chrome :从铬:

Right click "inspect" on the item you are trying to find the xpath.在您尝试查找 xpath 的项目上右键单击“检查”。

Right click on the highlighted area on the console.右键单击控制台上突出显示的区域。

Go to Copy xpath转到复制 xpath

selectElem=browser.find_element_by_xpath('x-path-here').click()

Reading Values Only仅读取值

from bs4 import BeautifulSoup

innerHTML = browser.execute_script("return document.body.innerHTML")
soup = BeautifulSoup(str(innerHTML.encode('utf-8').strip()), 'lxml')
value = soup.find('span', attrs={'class':'fxst-calendarpro fxst-table-s1'}).text

As the desired element is a dynamic element you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:由于所需元素是动态元素,您需要引入WebDriverWait以使所需元素可点击,您可以使用以下任一解决方案:

  • Using CSS_SELECTOR :使用CSS_SELECTOR

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.oe_menu_leaf[href*='/web#menu_id=']>span.oe_menu_text"))).click()
  • Using XPATH and text() :使用XPATHtext()

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='oe_menu_leaf' and starts-with(@href,'/web#menu_id=')]/span[@class='oe_menu_text' and text()='Reoni']"))).click()
  • Using XPATH and normalize-space() :使用XPATHnormalize-space()

     WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='oe_menu_leaf' and contains(@href,'/web#menu_id=')]/span[@class='oe_menu_text' and normalize-space()='Reoni']"))).click()
  • Note : You have to add the following imports :注意:您必须添加以下导入:

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

Reference参考

You can find a relevant detailed discussion in:您可以在以下位置找到相关的详细讨论:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 textContext 包含前导和尾随空白字符时如何单击按钮? - How to click on the button when the textContext contains leading and trailing white-space characters? 如何使用Selenium和Python定位元素并提取innerText - How to locate the element and extract the innerText using Selenium and Python 如何在 Selenium - Python 中通过它们的 innerText 定位元素 - How to locate elements by their innerText in Selenium - Python 如何使用 Selenium 和 Python 定位和单击 textarea 元素 - How to locate and click the textarea element using Selenium and Python 无法使用 Selenium 和 Python 定位和单击元素 - Cannot locate and click an element using Selenium and Python 搜索包含前导或尾随特殊字符的整个单词,例如 - 和=在python中使用正则表达式 - Searching for a whole word that contains leading or trailing special characters like - and = using regex in python Python如何使用分割定界符删除CSV上的空白 - Python how to use split delimiter to remove white-space on csv 如何使用Selenium Python定位元素 - How to locate the element using Selenium Python 如何使用 XPath Selenium 和 Python 定位元素 - How to locate the element using XPath Selenium and Python 熊猫修剪数据框中的前导和尾随空格 - Pandas trim leading & trailing white space in a dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM