【问题标题】:Python increment string number within selenium driver硒驱动程序中的Python递增字符串编号
【发布时间】:2021-03-05 07:56:46
【问题描述】:

大家好,根据我昨天分享的代码,我在通过这段代码时遇到了一个非常烦人的问题。

.py

number_to_loop_over = 5
page = number_to_loop_over
while True:
if page != 0:
    #page must be string so driver can be executed 
    driver.get(driver.current_url +str(page)) 

page = page - 1
    if page == 0:
    break

问题是我明白了: (current_url123) etc. #需要替换的页码,不要附加在旁边。

谢谢。

【问题讨论】:

    标签: python string selenium selenium-webdriver integer


    【解决方案1】:

    您每次都在调用当前网址:

    第一次迭代是baseurl,第二次迭代当前url是baseurl+id,第三次是baseurl+id+id

    所以不要将 id 附加到 driver.currenturl ,而是创建一个名为 baseurl 的变量并将初始基本 url 放入其中。

    number_to_loop_over = 5
    page = number_to_loop_over
    baseUrl = driver.current_url
    while True:
    if page != 0:
        #page must be string so driver can be executed 
        driver.get( baseUrl +str(page)) 
    
    page = page - 1
        if page == 0:
        break
    

    【讨论】:

    • 谢谢,我确实想过这个解决方案,但不知道为什么我没有尝试,反正这很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-03
    • 2018-08-27
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 2013-04-11
    相关资源
    最近更新 更多