1

I want to perform TAB action until I have reached a particular web-element. Until the active element is the below mentioned element, TAB action has to be performed.

>name = driver.find_element_by_name("name")
>name.send_keys("ABC")
>group = driver.find_element_by_name("group") 
>group.send_keys("DEF")

I am able to find element till the above state. After that, I want to perform TAB action until the below mentioned element is found. I guess using a loop would help.

elem = driver.find_element_by_css_selector('.PeriodCell input')

Please find below the HTML code

<div class="PeriodCell" style="left:px; width:112px;">
<div class="Effort forecasting">
<div class="entity field-value-copy-selected">
<input type="text" value="0.0" data-start="2014-09-20">
</div>
</div>
<div class="Effort unmet zero" title="">0.0
</div>
</div>

Please help. Thanks in Advance.

Abraham
  • 13
  • 2

2 Answers2

1

You can bring the element to the visible part of the screen by using one of the following methods.

  1. Using driver.execute_script("arguments[0].scrollIntoView();", element) you can read more about scrollIntoView() method here.

  2. Using the Actions class of selenium webdriver.

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_css_selector('.PeriodCell input')
actions = ActionChains(driver)
actions.move_to_element(element).perform()

You can read the difference between these two methods here

If you still need to use the TAB action to reach the element

from selenium.webdriver.common.keys import Keys

and using .send_keys(Keys.TAB) send the TAB key to the element

GPT14
  • 809
  • 6
  • 13
0

To perform TAB Action until you find a particular WebElement would not be as per the best practices. As per your comment the element is hidden so you need to bring the element within the Viewport first and then invoke click()/send_keys() as follows:

myElement = driver.find_element_by_xpath("//div[@class='PeriodCell']//input[@type='text'][@value=\"0.0\"]")
driver.execute_script("return arguments[0].scrollIntoView(true);", myElement)
# perfrom any action on the element

However the alternative using the TAB Action is as follows:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

global element
element = driver.find_element_by_name("name")
element.send_keys("ABC")
element = driver.find_element_by_name("group") 
element.send_keys("DEF")
while True:
    element.send_keys(Keys.TAB)
    element = driver.switch_to_active_element()
    if (element.get_attribute("type")=='text' and element.get_attribute("value")=='0.0' and element.get_attribute("data-start")=='2014-09-20'):
    print("Element found")
    break
# do anythin with the element
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352