Goal: use Selenium and Python to search for company name on LinkedIn's search bar THEN click on the "Companies" button in the navigation to arrive to information about companies that are similar to the keyword (rather than individuals at that company). See below for an example. "CalSTRS" is the company I search for in the search bar. Then I want to click on the "Companies" navigation button.
My Helper Functions: I have defined the following helper functions (including here for reproducibility).
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from random import randint
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome()
def li_bot_login(usrnm, pwrd):
##-----Log into linkedin and get to your feed-----
browser.get('https://www.linkedin.com')
##-----Find the Search Bar-----
u = browser.find_element_by_name('session_key')
##-----Enter Username and Password, Enter-----
u.send_keys(usrnm)
p = browser.find_element_by_name('session_password')
p.send_keys(pwrd + Keys.ENTER)
def li_bot_search(search_term):
#------Search for term in linkedin search box and land you at the search results page------
search_box = browser.find_element_by_css_selector('artdeco-typeahead-input.ember-view > input')
search_box.send_keys(str(search_term) + Keys.ENTER)
def li_bot_close():
##-----Close the Webdriver-----
browser.close()
li_bot_login()
li_bot_search('calstrs')
time.sleep(5)
li_bot_close()
Here is the HTML of the "Companies" button element:
<button data-vertical="COMPANIES" data-ember-action="" data-ember-action-7255="7255">
Companies
</button>
And the XPath:
//*[#id="ember1202"]/div[5]/div[3]/div[1]/ul/li[5]/button
What I have tried: Admittedly, I am not very experienced with HTML and CSS so I am probably missing something obvious. Clearly, I am not selecting / interacting with the right element. So far, I have tried...
companies_btn = browser.find_element_by_link_text('Companies')
companies_btn.click()
which returns this traceback:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Companies"}
(Session info: chrome=63.0.3239.132)
(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)
and
companies_btn_xpath = '//*[#id="ember1202"]/div[5]/div[3]/div[1]/ul/li[5]/button'
browser.find_element_by_xpath(companies_btn_xpath).click()
with this traceback...
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="ember1202"]/div[5]/div[3]/div[1]/ul/li[5]/button"}
(Session info: chrome=63.0.3239.132)
(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)
and
browser.find_element_by_css_selector('#ember1202 > div.application-outlet > div.authentication-outlet > div.neptune-grid.two-column > ul > li:nth-child(5) > button').click()
which returns this traceback...
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"#ember1202 > div.application-outlet > div.authentication-outlet > div.neptune-grid.two-column > ul > li:nth-child(5) > button"}
(Session info: chrome=63.0.3239.132)
(Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64)
It seem that you simply used incorrect selectors.
Note that
#id of div like "ember1002" is dynamic value, so it will be different each time you visit page: "ember1920", "ember1202", etc...
find_element_by_link_text() can be applied to links only, e.g. <a>Companies</a>, but not buttons
Try to find button by its text content:
browser.find_element_by_xpath('//button[normalize-space()="Companies"]').click()
With capybara-py (which can be used to drive Selenium), this is as easy as:
page.click_button("Companies")
Bonus: This will be resilient against changes in the implementation of the button, e.g., using <input type="submit" />, etc. It will also be resilient in the face of a delay before the button appears, as click_button() will wait for it to be visible and enabled.
Related
Some of my bots have this error today
Selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find dict 'desiredCapabilities'
(Driver info: chromedriver=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 10.0.22000 x86_64)
I've already tried some possibilities that I read in forums and in the selenium documentation. Example:
from selenium import webdriver
get = "198.0.0.1:4444/wd/hub"
.
capabilities = DesiredCapabilities.CHROME()
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"
driver = webdriver.Remote(desired_capabilities=capabilities
command_executor=get)
This problem occurs even in simpler code like:
from selenium import webdriver
driver = (way\chromedriver.exe')
driver.get('stackoverflow.com')
Does anyone know any documentation or solution for this problem?
i'm new to python selenium package. I'm developing crawler for a bookie site.
I'm unable to click and open an image link.
My code:
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
web = 'https://odibets.com/'
driver.get(web)
driver.implicitly_wait(3)
# btn = driver.find_element_by_css_selector('span.icon')""
btn = driver.find_element_by_xpath("//a[#href='/League'] and //span[text()='League']")
# <img src="https://s3-eu-west-1.amazonaws.com/odibets/img/menu/odi-league-2.png">
# //img[#src ="https://s3-eu-west-1.amazonaws.com/odibets/img/menu/odi-league-2.png"]
# //span[text()='League']
btn.click()
I get the following exception.
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression //a[#href='/League'] and //span[text()='League'] because of the following error: TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type. (Session info: chrome=96.0.4664.45) Stacktrace: Backtrace: Ordinal0 [0x010D6903+2517251]
Attached is the snapshot code from chrome developer tools and page itself.
Your href was /league not /League
driver.find_element_by_xpath("//a[#href='/league'] [.//span[contains(.,'League')]]").click()
This also works somehow the element wasn't clicking correctly.
elem=driver.find_element_by_xpath("(//a[#href='/league'] [.//span[contains(.,'League')]])[1]")
driver.execute_script("arguments[0].click()", elem)
I'm trying to run some tests automation on a web application (Angular2, https and websocket) with: gherkin,behave 1.2.6, selenium 3.141.0, firefox and python 3.9.4, geckodriver 0.29.1, allure-behave 2.8.40.
Configuration :
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:87.0) Gecko/20100101 Firefox/87.0"
Locator :
select = (By.XPATH, '//div[contains(#class,"connexion-content")]//select')
Error :
Assertion Failed: Item "profilselect.select" not found in current page, cannot set value on it
Captured logging:WARNING:root:[ACTION] waitForClickable => Element at "//div[contains(#class,"connexion-content")]//select" is not clickable.
Html option I want to select : Please note that « value » can’t be used because I’m only interest by the text between the « option » tag, is only if the given text correspond to a specific value that it is selected
Since your element itself is of type select why don't you use Selenium Select class where your can interact directly with it by text or index.
Since u have not shown your code, I can only help with pseudocode:
we_sel = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.ID, 'form-1-1'))
# The above statement is equivalent to below statement.
# If you are not familiar with WebDriver Waits then uncomment the below statement and comment the above statement.
# we_sel = driver.find_element_by_id('form-1-1')
sel = Select(we_sel)
sel.select_by_visible_text('replace withoption text u want to select')
References (Webdriver API Docs):
For Select class:
https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.select.html#module-selenium.webdriver.support.select
For WebDriverWait class:
https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.wait.html#module-selenium.webdriver.support.wait
For expected_conditions module:
https://www.selenium.dev/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html#module-selenium.webdriver.support.expected_conditions
I am learning Selenium using Python. I have tried to check the button status - whether enabled or not? For that I have used mercury demo site and wrote the below code. I am getting
Unable to locate element: input[value=roundtrip]
Code which I have used:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox(executable_path="C:\\Seleinum_Python\\WedDriver\\geckodriver.exe")
driver.get("http://newtours.demoaut.com/")
# Whether the user name & password enabled and displayed
user_ele = driver.find_element_by_name("userName") # <input type="text" name="userName" size="10">
print(user_ele.is_displayed()) # Return True /False based on element status
print(user_ele.is_enabled())
pwd_ele = driver.find_element_by_name("password") #<input type="password" name="password" size="10">
print(pwd_ele.is_displayed())
print(pwd_ele.is_enabled())
user_ele.send_keys("mercury")
pwd_ele.send_keys("mercury")
driver.find_element_by_name("login").click()
roundtrip_radio= driver.find_element_by_css_selector("input[value=roundtrip]") #<input type="radio" name="tripType" value="roundtrip" checked="">
print(roundtrip_radio.is_selected())
oneway_radio= driver.find_element_by_css_selector("input[value=oneway]")
print(oneway_radio.is_selected())
driver.close()
I even tried with below combination but still I am getting the same element not found issue.
roundtrip_radio= driver.find_element_by_css_selector("input[value='roundtrip']")
roundtrip_radio= driver.find_element_by_css_selector('input[value="roundtrip"]')
roundtrip_radio= driver.find_element_by_css_selector("//input[value='roundtrip']")
roundtrip_radio= driver.find_element_by_css_selector("input[value='roundtrip']")
roundtrip_radio= driver.find_element_by_css_selector('input[name="tripType"][value="roundtrip"]')
In your css selector you can use :checked to identify selected elements.
for example :-
"input[type=radio]:checked"
.Running my Selenium Script
I got the error:
**selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable**
What am I doing wrong?
CODE:
oCheckBox = browser.find_element_by_css_selector("input[value='para-mim']")
oCheckBox.click()
HTML
<input type="radio" name="para-quem" id="para-mim" value="para-mim">
Try this:
browser.find_element_by_xpath("//option[#value='para-mim']").click()
Try use .execute_script:
oCheckBox = browser.find_element_by_css_selector("input[value='para-mim']")
browser.execute_script("arguments[0].click();", oCheckBox)
Or use ActionChains:
from selenium.webdriver import ActionChains
oCheckBox = browser.find_element_by_css_selector("input[value='para-mim']")
action = ActionChains(browser)
action.move_to_element(oCheckBox).click(oCheckBox).perform()