-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathselenium_links.py
More file actions
46 lines (33 loc) · 1.54 KB
/
Copy pathselenium_links.py
File metadata and controls
46 lines (33 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# read the docs : https://selenium-python.readthedocs.io/
# find_element_by_link_text return first link !!!
# get attribute href
href = driver.find_element_by_link_text("some text").get_attribute("href")
print(href) # recomand.html
id = driver.find_element_by_link_text("some text").get_attribute("id")
print(id) # recommend_selenium_link
text = driver.find_element_by_id("selenium_link").text
print(text) # Recommend Selenium
tag_name = driver.find_element_by_id("selenium_link").tag_name
print(tag_name) # a
# by xpath
driver.find_element_by_xpath("//p/a[text()='some text']").click()
# find link by particial text
driver.find_element_by_partial_link_text("partial").click()
# click on second href with same name
driver.find_elements_by_link_text("Secod link with same name")[1].click()
# click on second link with same name
xpath = '//div[contains(text(), "Second")]/a[text()="Click here"]'
driver.find_element_by_xpath(xpath).click()
# when href open new window
current_url = driver.current_url
new_window_url = driver.find_element_by_link_text("Open new window").get_attribute("href")
driver.get(new_window_url)
driver.find_element_by_name("name").send_keys("sometext")
driver.get(current_url) # back