简体   繁体   English

Selenium 使用预期条件无法定位

[英]Selenium unable to locate though using expected conditions

Selenium is unable to locate the element by its xpath even though I'm using expected conditions to wait 3 seconds for element visibility. Selenium 无法通过其 xpath 定位元素,即使我使用预期条件等待 3 秒以获取元素可见性。 I'm trying to interact with the search bar, send a string, and press enter.我正在尝试与搜索栏交互,发送一个字符串,然后按回车键。 My code is below:我的代码如下:

def search_bar(self, search: str, xpath: str):
        """
        Looks for the search bar on the website and sends the search string

        Input: 
            search (str)
                the string to be search on
            xpath (str)
                the value of the xpath where the search is located
        

        """
        #search_bar = self.driver.find_element(by=By.XPATH, value=f'{xpath}')
        search_bar = self.wait.until(EC.element_to_be_clickable((By.XPATH, xpath )))
        search_bar.click()
        search_bar.send_keys(search)
        search_bar.send_keys(Keys.RETURN)

I think that it waits for the element to be clickable, then I can click it and send my search to it, (the website I am using is https://uk.trustpilot.com/ ).我认为它等待元素可点击,然后我可以点击它并将我的搜索发送给它,(我使用的网站是https://uk.trustpilot.com/ )。

scraper = Scraper()
scraper.load_webpage('https://uk.trustpilot.com/')
scraper.click_on_cookie('//*[@id="onetrust-accept-btn-handler"]')
scraper.search_bar('travel insurance', '//*[@id="heroSearchContainer"]')

Definition of my scraper class:我的爬虫 class 的定义:

class Scraper:
    def __init__(self):
        """
       Defines the webdriver for the scraper class
        
        """
        _edge_driver = os.path.join(os.getcwd(), 'msedgedriver.exe') #path to the edge driver
        options = Edgeoptions()
        options.add_argument('start-maximized')
        self.driver = webdriver.Edge(executable_path=_edge_driver, options= options)
        self.wait = WebDriverWait(self.driver,3)

Finally by the error message which I am receiving is the following:最后,我收到的错误消息如下:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

To send a character sequence to the search field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies :要将字符序列发送到search字段,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Code Block:代码块:

     from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC driver.get('https://uk.trustpilot.com/') WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#.netrust-accept-btn-handler"))).click() WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='query']"))).send_keys("Darman")
  • Browser Snapshot:浏览器快照:

达尔曼

  • 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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM