我一直在寻找解决这个问题的方法,但没有运气。
我正在使用selenium和BeautifulSoup4。
我将container_number变量传递给这个函数:
container_number="120001"
当我这样做时,找不到存储在container_number中的值(这对我没有意义)。变量container_number等于120001。
但是,如果我通过编写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)))
将container_number设置为120001
它运作得很好。
以下是我的相关代码:
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
xdriver@googlecode.com/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
driver@googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/rratclif/appdata/local/temp/tmpf3rtti/extensions/fxdr
iver@googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///c:/users/rratclif/appdata/local/temp/tmpf3rtti/extensions/fxdriver@go
运行我的脚本时,这是我的输出。
<aside>
<div>
<p>I WANT TO BE DIV</p>
</div>
<div>
<p>I WANT TO BE DIV</p>
</div>
</aside>
aside{
width: 100px;
height: 300px;
border: 1px solid blue;
color:black;
}
div{
width: 100px;
height: 100px
}
答案 0 :(得分:1)
这是猜测,但您可能需要在使用之前修剪值:
container_number = temp.find(text=True).strip()
find_container_number = wait.until(EC.presence_of_element_located((By.LINK_TEXT,container_number)))
或者,只需使用.get_text()
:
temp = items.findPrevious('a')
container_number = temp.get_text(strip=True)