简体   繁体   中英

Selenium: Not Finding Element/No Input

I am having an issue that is intermittent. I have noticed it about 40-50% of the time with this specific website. I am clueless as to why this is occurring. It is not linked to any specific browser. It happens with both firefox and chrome.

It was not happening before...

browser.get("https://www.voilanorbert.com")
inputName = wait.until(EC.presence_of_element_located((By.ID, "form-search-name")))
inputName.click()
inputName.send_keys(sheet.cell_value(i,0))
inputDomain  = wait.until(EC.presence_of_element_located((By.ID, "form-search-domain")))
inputDomain.click()
inputDomain.send_keys(sheet.cell_value(i,2))
norbertsearch = browser.find_element_by_name("search")
norbertsearch.click()

The code above is supposed to input names from an excel sheet and domain names from this excel sheet to the corresponding boxes. However, for whatever reason, on occassion this process gets hung up on selecting the domain box.

Here is a picture of what it looks like: 在此处输入图片说明

Usually there is no red box around the "@" sign in between. The process gets held up on inputting values to that box...and just times out.

So by hanging up, I mean it doesn't actually find the "domain" element. Thus, it doesn't input anything into the domain box, holding up the whole automation process and throws this exception: raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

The only good solution I have found to this problem is execute a javascript to set values.

        WebElement element = idriver.findElement(By.id(("form-search-domain")));

    ((JavascriptExecutor)idriver).executeScript("arguments[0].value = 'sample';", element);

Sorry the code sample is in Java. But all you have to do is execute javaScript document.findElementbyId("ID").value = "your value"

thinking that you excel has the data as shown below:

在此处输入图片说明

" test1 " is for - First and Last name textbox

" sample " is for - Domain.com textbox

so with in the code you should change from

inputDomain.send_keys(sheet.cell_value(i,2))

to

inputDomain.send_keys(sheet.cell_value(i,1))

since you are extracting the data (sample) from the 0th row and 1st column not the 2nd column

for you reference please go through the code below in detail:

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

browser = webdriver.Firefox()
book = xlrd.open_workbook("path_to_excelfile\\sample.xlsx")
sheet = book.sheet_by_name("Sheet1")
i=0
browser.get("https://www.voilanorbert.com")
inputName = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "form-search-name")))
inputName.click()
inputName.send_keys(sheet.cell_value(i, 0))
inputDomain = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "form-search-domain")))
inputDomain.click()
inputDomain.send_keys(sheet.cell_value(i, 1))
norbertsearch = browser.find_element_by_name("search")
norbertsearch.click()

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