简体   繁体   English

(初学者)如果python上的变量selenium

[英]( Beginner ) If variable selenium on python

I'm new to coding and I'm trying to learn webpage automation with Selenium.我是编码新手,正在尝试通过 Selenium 学习网页自动化。

So far I have managed to open a webpage, make it click where I want it to click and input any words I want.到目前为止,我已经设法打开一个网页,让它点击我想要点击的地方并输入我想要的任何单词。 However, I'm struggling with the if variable.但是,我正在为 if 变量而苦苦挣扎。

Basically I want to tell Selenium to click a button that I located by xpath. In turn this button should display a new element on the screen that I can also locate by xpath. However, sometimes when I click this button the new element doesn't display.基本上我想告诉 Selenium 单击我通过 xpath 找到的按钮。反过来,这个按钮应该在屏幕上显示一个新元素,我也可以通过 xpath 找到它。但是,有时当我单击这个按钮时,新元素不会展示。 So how can I tell Selenium that if the element doesn't display it should refresh the page and click until the new element is displayed and only then it can click on the new element?那么如何告诉Selenium,如果元素不显示,它应该刷新页面并点击直到显示新元素,然后才能点击新元素?

Code:代码:

from selenium import webdriver

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.common.keys import Keys

from selenium.common.exceptions import NoSuchElementException


driver = webdriver.Firefox()

url = "https://www.compumsa.eu/item/GV-R55XTOC-4GD-Gigabyte-Radeon-RX-5500-XT-4GB-OC-PCIE-9320"

driver.get(url)

driver.maximize_window()

click = driver.find_element_by_xpath('//*[@id="ContentPlaceHolderMain_LBAddItem"]')

click.click()

itempanier = driver.find_element_by_xpath('//*[@id="SpanCaddy"]')


if (itempanier.is_displayed()
  1. Use Explicit waits.使用显式等待。
  2. Use while infinite loop, and inside use try - except with find_elements , basically find_elements will return a list in Selenium-Python and check for it's size if >0 element will be visible if not, go to else block and reload the page and this will be repeating until it breaks from the while loop.使用 while 无限循环,并在内部使用try - except find_elements ,基本上find_elements将在Selenium-Python中返回一个列表并检查它的大小,如果>0元素将可见,如果不可见,go 否则阻止并重新加载页面,这将重复直到它从 while 循环中中断。

break only on two conditions :仅在两个条件下中断:

  1. If the if block condition is satisfied or some some reason code has gone into except block.如果满足 if 块条件或某些原因代码已进入 except 块。

Code:代码:

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://www.compumsa.eu/item/GV-R55XTOC-4GD-Gigabyte-Radeon-RX-5500-XT-4GB-OC-PCIE-9320")
click = wait.until(EC.element_to_be_clickable((By.ID, "ContentPlaceHolderMain_LBAddItem")))
click.click()

while True:
    try:
        itempanier = driver.find_elements_by_xpath('//*[@id="SpanCaddy"]')
        if len(itempanier) > 0:
            print('Meaning SpanCaddy web element is visible')
            # do some stuff like clicking on it and then break from the loop. 
            #break
        else:
            print('Meaning SpanCaddy web element is not visible')
            # do some other stuff like reload
            driver.refresh()
    except:
        print('Something went wrong')
        pass
        break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM