简体   繁体   中英

I can not access text box with selenium elements hid unless text clicked

I am working in python with Selenium. When I click on the line

 <div tabindex="0" class="page click-enterkey" style="text-align: center; margin: 0px 1em; float: left;">7</div>

and copy the path with a click() or then send.keys(8) it will not go to page 8 it just flashes and keeps on moving to next line of code. But, as a human, if I click the box it changes the elements to show that second picture with an input section.

I have no clue what to do

magicBox = driver.find_element('xpath','//*[@id="searchResults"]/div[1]/div/div[1]/div[2]/div[2]').click()
magicBox.send_keys('7')
magicBox.send_keys(Keys.RETURN)

I also tried

magicBox = driver.find_element('xpath','//*[@id="searchResults"]/div[1]/div/div[1]/div[2]/div[2]').click().send_keys('7')
magicBox.send_keys(Keys.RETURN)

Image

It seems like you are encountering an issue with the send_keys() method not working as expected after you have clicked the element with the click() method.

Here's what you can try to resolve the issue:

  1. First, make sure that you have imported the Keys module:

     from selenium.webdriver.common.keys import Keys
  2. After clicking the element, wait for the page to load before using send_keys() . You can use the WebDriverWait class and the expected_conditions module to wait for the element to be clickable before proceeding to the next step.

     from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC magicBox = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="searchResults"]/div[1]/div/div[1]/div[2]/div[2]'))).click() magicBox.send_keys('7') magicBox.send_keys(Keys.RETURN)

This should resolve the issue and allow you to successfully send the keys to the element.

Given the HTML you provided, you are trying to send_keys() to a DIV which is not valid. My guess is that if you click the box that contains the "7" in your screenshot, an INPUT HTML element will appear in the DOM that you can send_keys() a new value to.

You might also check the URL and see if ?page=7 or something similar is there and you can just edit the URL to take you directly to the page you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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