Russell
Russell

Reputation: 189

Selenium Python Binding can't pass variable into function

I have been searching for the solution to this problem for a while with no luck.

I am using selenium and BeautifulSoup4.

I pass the container_number variable into this function:

find_container_number=wait.until(EC.presence_of_element_located((By.LINK_TEXT,container_number)))

When I do this, the value stored in container_number is not found (which makes no sense to me). The variable container_number is equal to 120001. However, if I set container_number to 120001 by writing container_number="120001" it works just fine.

Here is my related code:

browser = webdriver.Firefox()

def change_location(number):
#print number
soup=bs(browser.page_source, 'html.parser')
found = len(soup.find_all(text=re.compile("Indefinite")))
if found>1:
    indef=soup.find_all(text=re.compile("Indefinite"))
    indef.pop(0)
    for items in indef:
        temp = items.findPrevious('a')
        #print temp
        container_number=temp.find(text=True)
        print container_number
        print type(container_number)
        container_number=str(container_number)
        print type(container_number)
        print container_number
        #container_number="120001" if i uncomment this line it works
        find_container_number=wait.until(EC.presence_of_element_located((By.LINK_TEXT,container_number)))

Here is my output when running my script.

120001
<class 'bs4.element.NavigableString'>
<type 'str'>
120001
Traceback (most recent call last):
  File ".\complete.py", line 86, in <module>
    change_location(number)
  File ".\complete.py", line 41, in change_location
    find_container_number=wait.until(EC.presence_of_element_located((By.LINK_TEXT,container_number)))
  File "C:\Python27\lib\site-packages\selenium-2.53.2-py2.7.egg\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/rratclif/appdata/local/temp/tmpf3rtti/extensions/f
[email protected]/components/driver-component.js:10770)
    at FirefoxDriver.prototype.findElement (file:///c:/users/rratclif/appdata/local/temp/tmpf3rtti/extensions/fxdriver@g
ooglecode.com/components/driver-component.js:10779)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/rratclif/appdata/local/temp/tmpf3rtti/extensions/fx
[email protected]/components/command-processor.js:12661)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/rratclif/appdata/local/temp/tmpf3rtti/extensions/fxdr
[email protected]/components/command-processor.js:12666)
    at DelayedCommand.prototype.execute/< (file:///c:/users/rratclif/appdata/local/temp/tmpf3rtti/extensions/fxdriver@go 

Upvotes: 1

Views: 205

Answers (1)

alecxe
alecxe

Reputation: 474003

It is a guess, but you may need to trim the value before using:

container_number = temp.find(text=True).strip()
find_container_number = wait.until(EC.presence_of_element_located((By.LINK_TEXT,container_number)))

Or, just use .get_text():

temp = items.findPrevious('a')
container_number = temp.get_text(strip=True)

Upvotes: 1

Related Questions