g1879 / DrissionPage

Python based web automation tool. Powerful and elegant.
https://DrissionPage.cn/
BSD 3-Clause "New" or "Revised" License
8.13k stars 773 forks source link

Help needed with switching tabs and finding similar functionality to Selenium's EC.invisibility_of_element_located #304

Open danielrzad opened 3 months ago

danielrzad commented 3 months ago

Hi guys,

I'm trying to switch from Selenium to DrissionPage, and I'm stuck on two things:

1) I don't know how to properly assign a new tab to the page after closing the initial tab. It's not a big issue, but the problem is probably due to my incompetence :smiley:. Below is a code example to better explain what I'm trying to do:

# Tabs action example
page.get('https://en.wikipedia.org/wiki/Castle')
init_tab = page.get_tab()

# Open a new tab
second_tab = page.new_tab()
second_tab.set.activate()
second_tab.get('https://www.youtube.com/')

# Close the first tab
init_tab.close()

# Now I want to set second_tab as the main tab for the page, but I don't know how

# The line below will raise an Error: DrissionPage.errors.PageDisconnectedError: 与页面的连接已断开。
page.get('https://www.amazon.com/')

2) In my Selenium project, I used Expected Conditions quite a lot. I mainly found the condition named EC.invisibility_of_element_located useful, which I used to check if a given element is not visible in the DOM. I know that DrissionPage has wait.ele_hidden() and wait.ele_deleted(), but the difference is that those methods will raise an error if they don't find the element at their startup. In Selenium, code like:

WebDriverWait(driver, 3).until(EC.invisibility_of_element_located(
    (By.XPATH, '//div[@class="clearfix diagnostic-wrapper"]/div[@class="ray-id"]')
))

returned True even if the specified element was never present in the DOM. I want to achieve something similar with DrissionPage. Maybe someone knows what approach I should use for this?

Best regards, Dan.

azmove commented 3 months ago

The function of page.close() is to close the current tab, not the browser, unless there is only one tab in the browser.

danielrzad commented 3 months ago

The function of page.close() is to close the current tab, not the browser, unless there is only one tab in the browser.

I know that. I just want to close the first (initial) tab and continue using second one as a main tab.